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.

Resources for Learning Azure from the Beginning

I can remember back when I first signed into the Azure portal. It was very overwhelming. I didn’t know where to click to even begin. Azure has changed a lot since then and still takes a lot of patience to learn, but once you get started it just kind of clicks.

When you’re starting to learn azure, one of the most important things is to have some great resources to use to get started! Having the right learning materials when you first sign into Azure can make a huge difference in your experience.

As tempting as it is to start learning the really fun stuff like AI, Cognitive services, Machine Learning, and other Azure services, you probably should start with learning the slightly more boring stuff first – Azure Infrastructure as a Service. These are things like Azure Networking, Azure Virtual Machines, and Azure Storage. Once you have these things down, you will have a much better understanding of the Azure basics. You will understand how Azure pieces things together and breaks things apart.

Below I am going to list some great resources for learning the very basics of Azure.

  • MoreAbout.Tech – Obviously this site in the Azure category would be my first stop 🙂
  • Pluralsight – Azure Fundamentals – If you haven’t heard of it, Pluralsight is a GREAT place to learn. It does cost some upfront, but they do usually offer a free trial. They have MANY courses on Azure and even courses specific to different sections of Azure. I would recommend starting with their Azure Fundamentals course.
  • Microsoft Learn – Azure Fundamentals – This is where I would recommend starting. Its a link to a certification test, but if you scroll down to Ways to prepare and click Self-paced, Microsoft links to several great learning paths where you can get started. It also even gives you a free lab environment. You can spin up resources and it doesn’t cost you anything at all! And at the end you can even try for the certification exam. The test was super easy, back when I took it, it was more on the theories of cloud computing rather than the specifics of Azure.
  • Azure Documentation – although not the best place to start with, this is by far one of the best places to learn once you understand the basics.
  • Azure for Student – If you can prove you are a student at a school or college, you can get free Azure credit and some of their most popular services for free. I would definitely sign up for this if you qualify!
  • Hands on experience is best! There are several ways to get hands on experience:
    • Use Microsoft Learn modules that offer a lab experience. These are free!
    • If you company has a MSDN license available, ask for one. This gives you $50 for free every month!
    • If your a student, you can get free credit
    • Ask you company if they use Azure. If so, ask for a walk around in Azure. Not only will this get you experience, but it will get you in with the Azure people. You could ask for some side duties if your company is open to cross training.
  • Azure Master Class on YouTube – a FREE YouTube playlist for an Azure masterclass