Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
User Powershell to Merge AppSettings from a production .config and a newly installed config
# I stumble around in PowerShell b/c I learn by doing
# back up the current config before install
# Pre-Install make a copy of the config
# Copy-Item J:\ElectSolve\WindowsServices\ServiceOrderService\ServiceOrderService.exe.config J:\ElectSolve\WindowsServices\ServiceOrderService\ServiceOrderService.exe.config.backup
# Post Install Loop over current config
# replace all connection strings and app.Values with the values from config.backup
# Notify us if there is a key which was not in the config.backup, a new key
$prodConfig_backup_path = 'C:\ElectSolve\WindowsServices\ServiceOrderService\ServiceOrderService.exe.config.backup'
$installedConfig_path = 'C:\ElectSolve\WindowsServices\ServiceOrderService\ServiceOrderService.exe.config'
$backup = (Get-Content $prodConfig_backup_path) -as [Xml]
$installed = (Get-Content $installedConfig_path) -as [Xml]
# TODO: Replace ConnectionString Keys from Prod to installed
# Replace ApplicationSetting Keys from Prod to installed
foreach( $backup_add in $backup.configuration.appSettings.add)
{
foreach( $installed_add in $installed.configuration.appSettings.add)
{
if ($backup_add.Key -eq $installed_add.Key)
{
if ($backup_add.Value -ne $installed_add.Value)
{
Write-Output "Found matching environment variable for key: $($backup_add.key)"
Write-Output "Replacing value $($installed_add.value) with $backup_add.Value"
$installed_add.value = $backup_add.Value
}
}
}
}
# Check the type of the var so I can create an array from what...? Oh I see it's and object... oh joy
write-host "GetType $($backup.configuration.appSettings.add.GetType())"
# ok let's see what happens when we dump to an array
$array = @()
$array += $backup.configuration.appSettings.add
$array | Format-Table
# This is a fast hack since powershell has no slices... boo
# so I can get an array with one column to call .Contains() on.
# There has to be a better way?!
$backupAppSettings = @()
for ($i=0; $i -lt $array.length; $i++) {
write-host "Array Loop $($array[$i].key)"
$backupAppSettings += $array[$i].key
}
# Finally look for new keys and alert someone... maybe we can just write file saying new keys... review me
foreach( $installed_add in $installed.configuration.appSettings.add)
{
#write-host $backup.configuration.appSettings.add.Contains($installed_add.Key)
if(!$backupAppSettings.Contains($installed_add.Key))
{
write-host "New Key Found for $($installed_add.Key):$($installed_add.Value) is a new key. Please Review this key."
}
}
# Save the installed Config
$installed.Save($installedConfig_path)
# We can remove the .backup or just timestamp the end of the config for history.
# This will allow us to run multiple installs in the future and beyond.
Rename-Item -Path $prodConfig_backup_path -NewName $prodConfig_backup_path-$(Get-Date -Format "yyyyMMdd-HHmmss")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment