Script to demonstrate hashtable merging in powershell and updating web applications in azure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($websitename = 'TEst' ,$resourceGroup = 'SchuTest',$slot = 'production', $appSettings ='{"AppSettings:testkey1": "45test","AppSettings:TestId": "This is a Test Key 28"}') | |
#https://stackoverflow.com/questions/8800375/merging-hashtables-in-powershell-how | |
Function Merge-Hashtables([ScriptBlock]$Operator) { | |
$Output = @{} | |
ForEach ($Hashtable in $Input) { | |
If ($Hashtable -is [Hashtable]) { | |
ForEach ($Key in $Hashtable.Keys) {$Output.$Key = If ($Output.ContainsKey($Key)) {@($Output.$Key) + $Hashtable.$Key} Else {$Hashtable.$Key}} | |
} | |
} | |
If ($Operator) {ForEach ($Key in @($Output.Keys)) {$_ = @($Output.$Key); $Output.$Key = Invoke-Command $Operator}} | |
$Output | |
} | |
try { | |
foreach($website in $websiteName) | |
{ | |
ConvertFrom-Json $appSettings -ErrorAction Stop | |
#it is expected that the app settings is a string representation of a hashtable that is writtin in Json. So that it can be converted to a powershell hashtable during runtime | |
$newAppSettings = $appSettings | convertfrom-json | |
$newAppSettingsHash = @{} | |
$newAppSettings.psobject.properties | ForEach-Object { $newAppSettingsHash[$_.Name] = $_.Value } | |
$Application = get-azurermwebappslot -Name $website -ResourceGroupName $resourceGroup -Slot $slot | |
$ExistingSettings = $Application.siteconfig.AppSettings | |
$appSettingsHash = @{} | |
foreach($k in $ExistingSettings) | |
{ | |
$appSettingsHash[$k.name] = $k.value | |
} | |
#https://stackoverflow.com/questions/8800375/merging-hashtables-in-powershell-how | |
$hashtable = $newAppSettingsHash, $appSettingsHash | Merge-Hashtables {$_[0]} | |
$results = Set-AzureRmWebAppSlot -AppSettings $hashtable -name $website -ResourceGroupName $resourceGroup -slot $slot | |
$r = $results.SiteConfig.AppSettings | |
Write-Output $r | |
} | |
} | |
catch | |
{ | |
Write-Error "$appsettings must be in JSON format" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment