0 Comments

I came across a situation where I needed to change the AppInsights instrumentation key for a bunch of web apps that one of my client has. We wanted to have all the web apps to have the same AppInsights instrumentation key, so I figured that it would a good use case for Azure CLI. I could have just copy pasted the values for every app using the Azure portal, but there we’re many apps, and I’m stubborn!


In order to add or update a setting for a web app the command that needs to be invoked is:

az webapp config appsettings set --name $appName --resource-group $yourResGroup --settings APPINSIGHTS_INSTRUMENTATIONKEY=SOME_KEY

If you needed to set more than on setting you could just provide a space separated list of items in the form of key=value

Now that command will set the new key in the desired webApp….. but I have a bunch of them to update, no way I’m doing all of them by copy pasting this line for every single one of my apps. After some digging in the docs I almost found what I was looking for a command to list all  webaps:

[sourcecode language='powershell' ]
az webapp list [--resource-group]  --out tsv
[/sourcecode]

That will give me a nice list of all the web apps in my subscription. Since I’m not a scripting (apologies for any non-sense I could write), I was kinda hitting a wall… I can display a list of what I need but how the heck can I loop through that list and set that setting. Well it turns out that I could just declare a variable to capture the output instead of having it display on the screen. So after some tweaking this is what I ended up with:

[sourcecode language='powershell' ]
 $webApps = az webapp list --query "[].{ name: name}" --out tsv
    $resGroup = "sharegate-ops-$env"
    $webApps | ForEach{
        Write-Host "Updating $_"
        az webapp config appsettings set --name $_ --resource-group $resGroup --settings APPINSIGHTS_INSTRUMENTATIONKEY=SOME_KEY
    }
[/sourcecode]

And that was it, all my webapps we’re updated, there is a massive amount of scripts samples and explanation on the Azure CLI docs and I would suggest looking at it every now and then


[sourcecode language='powershell'  padlinenumbers='true']
az webapp config appsettings set --name $appName --resource-group $yourResGroup --settings APPINSIGHTS_INSTRUMENTATIONKEY=SOME_KEY
[/sourcecode]