Apply / Update application settings for Azure App Service using PowerShell

Windows Azure App Service (Now an umbrella term for Azure Web App, Azure Api App, etc.) has a handy capability whereby developers can store key-value string pairs in Azure as part of the configuration information associated with a website.  At runtime, Windows Azure Web Sites automatically retrieves these values for you and makes them available to code running in your website.  Since the key-value pairs are stored behind the scenes in the Windows Azure Web Sites configuration store, the key-value pairs don’t need to be stored in the file content of your web application.  From a security perspective that is a nice side benefit since sensitive information such as Sql connection strings with passwords never show up as cleartext in a config file. However, sometimes, this can be a little too much for the Azure Admins to configure each setting over there. In this blog post, we’ll learn how to apply application settings using PowerShell. 

If you’d like to apply these settings using Azure ARM, you can read my previous post here.

Fetch Existing Web App using PowerShell

Let’s assume that you already have an existing web app in Azure under a subscription, and you have logged in powershell and set azure resource manager context. Now, first step is to get existing web app inside a variable so that we can work on it during later steps.  For this, we can use below code:

$webAppName = “metavrse01”
$resourceGroupName = “metavrse-webapps-rg”

where, $webAppName is the name of azure web app in reference and $resourceGroupName is the name of resource group which contains web app. Then we can use cmdlet Get-AzureRmWebApp to get web app:

$webApp = Get-AzureRmWebApp -ResourceGroupName $resourceGroupName -Name $webAppName

Fetch existing web app and assign to a variable inside PowerShell
Fetch existing web app and assign to a variable inside PowerShell

Viewing Existing App Settings

Since app settings are a part of site configuration property in the web app, we can view them simply by using $webApp.SiteConfig.AppSettings command:

View exising app settings
View exising app settings

Apply New App Settings

Since app settings are basically key value pairs, we would need to store new settings using a hashtable first. Let’s define an hashtable as below:

$newAppSettings = @{“newSetting01″=”newValue01″;”newSetting02″=”newValue02″;”newSetting03″=”newValue03”}

and verify it’s properly created by trying to fetch one or two keys:

Create new hashtable using PowerShell
Create new hashtable using PowerShell

We can then apply the settings to Azure Web App using below command:

Set-AzureRmWebApp -AppSettings $newAppSettings -Name $webAppName -ResourceGroupName $resourceGroupName

Apply new app settings to azure web app
Apply new app settings to azure web app

We can now verify the same from Azure Portal:

Checking application settings using Azure Portal
Checking application settings using Azure Portal

Modify / Add New App Settings

Note that we have lost previous settings using this method. So we have to add little tweak to our method. First, let’s fetch existing app settings the same as previous and store inside a variable:

$appSettings = $webapp.SiteConfig.AppSettings

Storing app settings inside a variable
Storing app settings inside a variable

We can then run a loop to convert it into a hashtable:

$newAppSettings = @{}
ForEach ($item in $appSettings) {
$newAppSettings[$item.Name] = $item.Value
}

Once we have the same, we can modify existing values and new values using hashtable syntax. For example, in our case like below:

$newAppSettings.WEBSITE_NODE_DEFAULT_VERSION = “6.9”
$newAppSettings[‘newAppSetting01’] = “newSettingValue01”
$newAppSettings[‘newAppSetting02’] = “newSettingValue02”

And finally, we can set it using Set-AzureRMWebApp cmdlet as mentioned above. We can again verify the same from Azure Portal:

Check application settings using Azure Portal-2
Check application settings using Azure Portal-2

In a similar way, we can copy existing application settings from one Azure Web App and apply it to other Azure Web Apps as well.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s