Blog Post

Azure APP Update configuration

Monday, July 3, 2017 12:36 AM

Azure App

I am working on my project where I would like to update Azure app using PowerShell.

I do not want to reinvent the wheel and therefore I wanted the reusable script I can use many times across multiple clients.  

 

Retrieve App Settings

First set these two variables.

$myResourceGroup = 'RESOURCE_GROUP_NAME'
$mySite = 'SITE_NAME'

Then switch to the new Resource Manager mode and sign-in to your account.

Switch-AzureMode AzureResourceManager
Get-AzureAccount

Then retrieve the app settings. (Note that a back tick (`) means a new line.)

(Invoke-AzureResourceAction -ResourceGroupName $myResourceGroup `
 -ResourceType Microsoft.Web/sites/Config -Name $mySite/appsettings `
 -Action list -ApiVersion 2017-07-01 -Force).Properties

Add/Update App Settings

To update settings, first put them into a variable.

$props = (Invoke-AzureResourceAction -ResourceGroupName $myResourceGroup `
 -ResourceType Microsoft.Web/sites/Config -Name $mySite/appsettings `
 -Action list -ApiVersion 2018-07-01 -Force).Properties

To use Set-AzureWebsite convert the variable to a hash table.

 $hash = @{}
 $props | Get-Member -MemberType NoteProperty | % { $hash[$_.Name] = $props.($_.Name) }

Now add/update values in the hash table.

$hash.NewKey = "NewValue"
$hash.ExistingKey = "NewValue"

Then switch back to Service Management mode and commit the settings.

Switch-AzureMode AzureServiceManagement
Set-AzureWebsite -Name $mySite -AppSettings $hash

Complete Code Listing

$myResourceGroup = 'RESOURCE_GROUP_NAME'
$mySite = 'SITE_NAME'

Switch-AzureMode AzureResourceManager
Get-AzureAccount

(Invoke-AzureResourceAction -ResourceGroupName $myResourceGroup `
 -ResourceType Microsoft.Web/sites/Config -Name $mySite/appsettings `
 -Action list -ApiVersion 2017-07-01 -Force).Properties

$props = (Invoke-AzureResourceAction -ResourceGroupName $myResourceGroup `
 -ResourceType Microsoft.Web/sites/Config -Name $mySite/appsettings `
 -Action list -ApiVersion 2017-07-01 -Force).Properties

 $hash = @{}
 $props | Get-Member -MemberType NoteProperty | % { $hash[$_.Name] = $props.($_.Name) }

$hash.NewKey = "NewValue"
$hash.ExistingKey = "NewValue"

Switch-AzureMode AzureServiceManagement
Set-AzureWebsite -Name $mySite -AppSettings $hash

Notes

The AzureServiceManagement and AzureResourceManager are not meant for use in the same session. For now, the latter does not seem to permit updating the app settings via Set-AzureResource. The above is a workaround. Another way is to use the Azure CLI instead of PowerShell.

Blog
Products
Error pages
Links and tools