Mike Streetz (O_P)
03/18/2024, 8:57 PMMike Streetz (O_P)
03/18/2024, 9:05 PMMike Streetz (O_P)
03/18/2024, 9:25 PMJon Bucud
03/18/2024, 9:27 PMJon Bucud
03/18/2024, 9:27 PMMike Streetz (O_P)
03/18/2024, 9:28 PMJon Bucud
03/18/2024, 9:28 PM# 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."
}Mike Streetz (O_P)
03/18/2024, 9:29 PMJon Bucud
03/18/2024, 9:37 PM