I need to Re-IP a VPX that has 2000 lines of confi...
# citrix-netscaler
m
I need to Re-IP a VPX that has 2000 lines of config, I have the old and new IPs, is there a tool/script that will do multi find and replace if given a CSV of old values and new values to replace them with? Maybe a Notepad++ plugin?
so that worked and just saved me a ton of time
j
doh
im too slow
m
have you used it before?
j
Copy code
# Load System.Windows.Forms assembly
Add-Type -AssemblyName System.Windows.Forms

# Function to prompt user to select a file using Windows Forms OpenFileDialog with auto-completion
function Select-File {
    param (
        [string]$Prompt
    )

    $fileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $fileDialog.Title = $Prompt
    $fileDialog.Filter = "All files (*.*)|*.*"

    if ($fileDialog.ShowDialog() -eq 'OK') {
        return $fileDialog.FileName
    } else {
        Write-Error "File selection canceled by user."
        exit
    }
}
# Function to prompt user to select or create a file using Windows Forms SaveFileDialog with auto-completion
function SaveNew-File {
    param (
        [string]$Prompt
    )

    $fileDialog = New-Object System.Windows.Forms.SaveFileDialog
    $fileDialog.Title = $Prompt
    $fileDialog.Filter = "All files (*.*)|*.*"

    if ($fileDialog.ShowDialog() -eq 'OK') {
        return $fileDialog.FileName
    } else {
        Write-Error "File selection canceled by user."
        exit
    }
}


# Prompt user for CSV file containing old and new IPs
$csvFile = Select-File -Prompt "Select the CSV file containing old and new IPs"

# Prompt user for config file
$configFile = Select-File -Prompt "Select the config file"

# Load CSV data
$ipMappings = Import-Csv -Path $csvFile

# Read config file content
$configContent = Get-Content -Path $configFile

# Create an empty array to store changed lines
$changedLines = @()

# Iterate through each mapping
foreach ($mapping in $ipMappings) {
    # Get the old and new IPs
    $oldIp = $mapping.OldIP
    $newIp = $mapping.NewIP

    # Replace all occurrences of old IP with new IP in the config content
    $changedContent = $configContent -replace [regex]::Escape($oldIp), $newIp

    # Compare the original content with the changed content to identify modified lines
    $changedLines += Compare-Object $configContent $changedContent | Where-Object { $_.SideIndicator -eq '=>' } | ForEach-Object { $_.InputObject }
}

# Show what changes would be made
Write-Host "Changes that would be made:"
foreach ($line in $changedLines) {
    Write-Host $line
}

# Prompt user for output file name
$outputFileName = SaveNew-File -Prompt "Please enter the output file name"

# Prompt user for confirmation before writing changes to a new config file
$confirm = Read-Host "Do you want to apply these changes? (yes/no)"
if ($confirm -eq "yes") {
    # Write changes to a new config file
    $changedContent | Set-Content -Path $outputFileName
    Write-Host "Changes applied and saved to $outputFileName"
} else {
    Write-Host "Changes not applied."
}
m
oh cool
j