Automatically set Azure app settings

The bigger your applications are, the more settings you’ll have in the web.config file. If you run your webapp in Azure and there are a lot of settings, then it’s annoying to set all the values manually. It would be much easier, if Azure would automatically show all app settings in the section in the azure portal and you just need to adapt them. Unfortunately there this doesn’t work out of the box and therefore the application settings will look like:

AzureApplicationSettings

If we want to have all settings from the web.config file available in the application settings, then we can add them manually or via Powershell.

As I prefer the Powershell, I wrote a script that reads all application settings from a config file, e.g. the web.config, and writes them to the azure web application. The script at first shows all available values. After that, it asks if it should add all missing values and overwrite all existing values. If you choose no, then it asks, if it should add only the missing ones. If you choose yes, then the settings are updated. This makes it easy to use if you have multiple environments with different settings.

Script to update all settings

$resourceGroup  = "MyResourceGroup"
$webAppName     = "MyWebApp"
$subscriptionId = "MySubscriptionId"
$appConfigName  = "web.config"
$slot           = "[DeploymentSlot or empty]"
 
function SetSubscription([string] $subscriptionId) {
    try { $context = Get-AzureRmContext -ErrorAction SilentlyContinue } catch { }
    if(!$context) {
        Add-AzureRmAccount # set azure account and select subscription
    }
    if($context.Subscription.SubscriptionId -ne $subscriptionId) {
        Set-AzureRmContext -SubscriptionID $subscriptionId
    }
}
 
function UpdateSettings($hash) {
    if([String]::IsNullOrEmpty($slot)) {
        Set-AzureRmwebApp -ResourceGroupName $resourceGroup -Name $webAppName -AppSettings $hash
    }
    else {
        Set-AzureRmwebAppSlot -ResourceGroupName $resourceGroup -Name $webAppName -AppSettings $hash -Slot $slot
    }
}
 
try
{
    Write-Host "load config file..."
    # load config file
    $appConfig = New-Object XML
    $appConfig.Load($appConfigName)
 
    Write-Host "set subscription..."
    SetSubscription($subscriptionId)
 
    Write-Host "load webApplication and settings..."
    $webApp = Get-AzureRmwebApp -ResourceGroupName $resourceGroup -Name $webAppName
    $webAppSettings = $webApp.SiteConfig.AppSettings
 
    $hash = @{}
 
    Write-Host "The following application settings are already available in the Azure webapp '$webAppName':"
    $webAppSettings | ft
 
    # copy existing app settings to hash
    foreach ($setting in $webAppSettings) {
        $hash[$setting.Name] = $setting.Value
    }
 
    Write-Host "------------------------------------------------------------------------"
    Write-Host "Settings from config file '$appConfigName':"
 
    $appSettings = $appConfig.configuration.appSettings.add
    $appSettings | ft
 
    # copy app settings from config file to hash
    foreach ($appSetting in $appSettings) {
        $hash[$appSetting.key] = $appSetting.value
    }
 
    Write-Host "------------------------------------------------------------------------"
    Write-Host "Do you want to add all missing settings and update the existing ones? (y/n) [Default: n]"
    $input = Read-Host
 
    if($input -eq "y") {
        UpdateSettings($hash)
    }
    else {
        Write-Host "Do you want to add only the values that are NOT already set in the web app (see list below)? (y/n) [Default: n]"
        foreach($setting in $webAppSettings) {
            $hash.Remove($setting.Name);
        }
        $hash
        $input = Read-Host
        if($input -eq "y") {
            UpdateSettings($hash)
        }
    }
}
catch {
    Write-Host "An error occurred and the script stopped"
    Write-Error $_.Exception.Message

Categories:

One response

  1. Hi,

    Thanks for your script. It works great for Application Settings. I’d like to do the same for Connection Strings: I modified a bit the script to handle connection strings but I get an error ” Connection
    string type value pair must be of type ‘System.Collections.Hashtable'” when it goes to update the connection strings with the ones set in the config file.
    It looks like the connection strings retrieved in the config file are not set as hashtable. Do you have any idea how to have it done ?

    Thank you for the tips !

Leave a Reply

Your email address will not be published. Required fields are marked *

About
about armin

Armin Reiter
Blockchain/Web3, IT-Security & Azure
Vienna, Austria

Reiter ITS Logo

Cryptix Logo

Legal information