Update Azure Web App Connection Strings Using PowerShell

I needed to update the connection strings on several Azure Web Apps and Azure Functions. This turned out being harder than I expected because of the way Azure expect hashtables and it removes connection strings not included. I set out to create a re-usable function that I can use to change these connection strings easily. Below is my PowerShell function and how to use it.

When using PowerShell to update these, PowerShell overwrites all connection strings with the new list. Because of this, you need to grab the current connection strings first. When using this function, you pass the current connection strings to it, the new connection strings and it will take it from there.

The function will create a Hashtable (which is how Azure requires it to be passed to the API) of all the old connection strings. From there, it will take your hastable of new connection strings and add or overwrite any old ones.

Use this script at your own risk! Remember updating environment variables and connection strings does restart the app instantly!

function Add-ConnectionStrings {
    param(
        $currentConnectionStrings,
        $newConnectionStrings
    )
    $connectionStrings = @{}

    # Setting the config requires a Hastable. Then generates a hastable of the current connection strings and adds/updates the new one
    foreach ($c in $currentConnectionStrings) {
        $connectionStrings[$c.Name] = 
            @{
                Value = $c.ConnectionString;
                Type = ($c.Type | Out-String).ToUpper()
            }
    }
    foreach ($n in $newConnectionStrings) {
        $connectionStrings[$n.Name] = 
            @{
                Value = $n.ConnectionString;
                Type = ($n.Type | Out-String).ToUpper()
            }
    }

    return $connectionStrings
}

Examples of this function being used looks like this:

# Build out the new connection strings
$newCS = @(
    @{
        Name = "ConnectionString1";
        Value = "LongConnectionString";
        Type = "Sql";
    },
    @{
        Name = "ConnectionString1";
        Value = "LongConnectionString";
        Type = "Sql";
    }
)    
# Get the current connection strings so we don't get rid of ones that already exists
$currentConnectionStrings = (Get-AzWebApp -ResourceGroupName "rg" -Name "WebAppName").SiteConfig.ConnectionStrings

# Use the function to combine/overwrite the connection strings and return them in a way Azure can understand
$newConnectionStrings = Add-ConnectionStrings -currentConnectionStrings $currentConnectionStrings -newConnectionStrings $newCS

# Actually update the connection strings on the Web App
Set-AzWebApp -ResourceGroupName "rg" -Name "WebAppName" -ConnectionStrings $newConnectionStrings 

For security, its important you do not save your connection strings in plaintext to disk. You should store these connection strings in an Azure Key Vault or other safe place, or grab them on the fly and only use them in memory.