Skip to content

Instantly share code, notes, and snippets.

@crshnbrn66
Created October 20, 2017 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crshnbrn66/90d4982a81267d7595aa39a1ad922bf3 to your computer and use it in GitHub Desktop.
Save crshnbrn66/90d4982a81267d7595aa39a1ad922bf3 to your computer and use it in GitHub Desktop.
Script to demonstrate hashtable merging in powershell and updating web applications in azure
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