Cloud Skills Blog

Closing the Skills Gap One Student at a Time

Easily Manage Azure Web App Connection Strings using PowerShell

Posted on: January 20, 2017 by Chris Pietschmann

The Application Settings of an Azure Web App are really powerful. They allow you to easily configure App Settings (key/value pairs) and database Connection Strings for an application directly within Azure. These values stored in the Azure Web App Application Settings will override any App Settings or Connection String values deployed within an ASP.NET applications web.config file. They are even made available as Environment Variables for other non-.NET platforms that Azure App Service supports. This functionality allows for these values to reside within the Environment rather than rely on being deployed from CI/CD builds correctly. You also get the added benefit that your developers are isolated from having access to Production database password and other potentially sensitive Production configuration values. This article covers how you can use the Azure PowerShell cmdlets to add, set, and manage an Azure Web Apps Connection Strings. This allows you to automate the process of updating these settings, and can be used to automate the update of multiple Azure Web Apps that may need to always be pointed to the same databases.

Quickly Set Connection Strings with PowerShell

Setting the Connection Strings for an Azure Web Apps Application Settings is fairly simple. Once you authenticate Azure PowerShell with your Azure Subscription, you can then use the Set-AzureRmWebAppSlot cmdlet to pass in a set of one or more Connection Strings to be set on a particular Azure Web App and specify the Deployment Slot to update. This cmdlet uses the Azure Resource Manager APIs to perform the update.

Set-AzureRmWebAppSlot -ResourceGroupName "MyResourceGroup" -Name "MyWebApp" -Slot production -ConnectionStrings $hash

Here are short descriptions of what the different parameters of the cmdlet are and what values they are expecting:

  • ResourceGroupName – The full name of the Resource Group within your subscription where the Web App resides.
  • Name – The full name of the Azure Web App that you are attempting to set the Application Settings for.
  • Slot – The name of the Deployment Slot to update.
  • ConnectionStrings – The set of Connection String values that you are saving. This needs to be a PowerShell Hash object that contains the data.

As you can see, the above cmdlet is fairly simple to use. One thing that can seem tricky at first glance is the use of a Hash for the ConnectionStrings parameter. However, it is fairly simple to create a new Hash object in PowerShell, and then subsequently add some Connection String “objects” to it.

Here’s a simple example of creating a PowerShell Hash object, and adding a Connection String value to it:

# Create Hash variable for Connection Strings
$hash = @{}

# Add a Connection String to the Hash by using a Hash for the Connection String details
$hash["AppConnString"] = @{ Type = "SqlAzure"; Value = "conn-string-here" }

# Save Connection String to Azure Web App
Set-AzureRmWebAppSlot -ResourceGroupName "MyResourceGroup" -Name "MyWebApp" -Slot production -ConnectionStrings $hash

The use of the Set-AzureRmWebApp cmdlet is simple, and so is the use of a PowerShell Hash object to easily create a collection of Connection Strings. The above example is creating a Connection String named “AppConnString” for a SQL Azure Database. The other supported Connection Strings Types that are supported within the Azure Portal are also supported through this method as well.

Alternatively, the Get-AzureRmWebApp cmdlet could also be used to just update the Production Deployment Slot only.

Correctly Set Connection Strings with PowerShell

Something to be aware of when setting the Connection Strings for an Azure Web App using PowerShell is actually a little more complicated than the above code snippet. In fact, if you run that snippet, it will remove all other Connection Strings in such that the “AppConnString” will actually be the only Connection String that will remain. Now this can cause serious issues if you want to just update a single Connection String, but keep all others intact.

In order to update a single Azure Web Apps connection string while maintaining all other connection strings, you need to pass in the values for ALL the Connection Stings in the save call using the Set-AzureRmWebApp cmdlet. To do this you must first iterate through the entire list of existing Connection Strings and add them to the Hash collection before adding or setting specific Connection String values on the Hash collection.

Here’s the full script necessary that will ensure that none of the existing connection strings are removed when making an update to one or more connection strings on an Azure Web App:

# Load Existing Web App settings
$webApp = Get-AzureRmWebAppSlot -ResourceGroupName "MyResourceGroup" -Name "MyWebApp" -Slot production

# Get reference to the existing Connection Strings
$existingConnectionStrings = $webApp.SiteConfig.ConnectionStrings

# Create Hash variable for Connection Strings
$hash = @{}

# Copy over all Existing Connection Strings to the Hash
ForEach($connString in $existingConnectionString) {
$hash[$connString.Name] = @{ Type = $connString.Type.ToString(); Value = $connString.ConnectionString }
}

# Add or Update a desired Connection String within the Hash collection
$hash["AppConnString"] = @{ Type = "SqlAzure"; Value = "conn-string-here" }

# Save Connection String to Azure Web App
Set-AzureRmWebAppSlot -ResourceGroupName "MyResourceGroup" -Name "MyWebApp" -Slot production -ConnectionStrings $hash

This is an important tip to remember when adding or updating Connection Strings on an Azure Web App using the PowerShell cmdlets. If you forget you may just accidentally delete all the other Connection Strings for the application in the event there are more existing Connection Strings than you are adding or updating. Don’t break production, and please remember to use this tip!

leave a reply

Share this page

Categories

Connect with Opsgility