2

I wanted to change the azure webapp settings and i was able to do it through the following powershell code. As you can see there are variables and their values, I want to dynamically call the Variables and their values and loop through it and by using the command only once. Is there a way to achieve it

az webapp config appsettings set -g $resourceGroup -n $webAppName --settings ApplicationInsightsAgent_EXTENSION_VERSION="~2"
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings APPINSIGHTS_PROFILERFEATURE_VERSION="1.0.0" 
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings APPINSIGHTS_SNAPSHOTFEATURE_VERSION="1.0.0"
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings XDT_MicrosoftApplicationInsights_BaseExtensions="disabled"
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings XDT_MicrosoftApplicationInsights_Mode="recommended"
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings DiagnosticServices_EXTENSION_VERSION="~3"
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings SnapshotDebugger_EXTENSION_VERSION="disabled"
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings InstrumentationEngine_EXTENSION_VERSION="disabled"
Philip
  • 21
  • 2

2 Answers2

2
  • The az CLI accepts multiple key-value pairs as arguments to its --settings parameter.

  • PowerShell lets you pass the elements of an array as individual arguments to an external program (such as the az CLI).

# Define the settings as an array of key-value pairs 
# as strings in format
#   "<key>=<value"
# Use variable references inside the expandable strings ("...") as needed. 
$settings = 
  "ApplicationInsightsAgent_EXTENSION_VERSION=~2",
  "APPINSIGHTS_PROFILERFEATURE_VERSION=1.0.0",
  "APPINSIGHTS_SNAPSHOTFEATURE_VERSION=1.0.0",
  "XDT_MicrosoftApplicationInsights_BaseExtensions=disabled",
  "XDT_MicrosoftApplicationInsights_Mode=recommended",
  "DiagnosticServices_EXTENSION_VERSION=~3",
  "SnapshotDebugger_EXTENSION_VERSION=disabled",
  "InstrumentationEngine_EXTENSION_VERSION=disabled"

# Pass the array to --settings-names
az webapp config appsettings set -g $resourceGroup -n $webAppName --settings $settings

Caveat: CLI az is implemented as az.cmd, i.e. a batch file (which calls Python), so arguments passed to it may require escaping to satisfy cmd.exe's syntax requirements.

mklement0
  • 382,024
  • 64
  • 607
  • 775
1

simplest way of doing this with powershell + az cli:

'ApplicationInsightsAgent_EXTENSION_VERSION="~2"',xxx,'InstrumentationEngine_EXTENSION_VERSION="disabled"' | Foreach-Object {
    az webapp config appsettings set -g $resourceGroup -n $webAppName --settings $_
}

you can also achieve the same without powershell with xargs

4c74356b41
  • 69,186
  • 6
  • 100
  • 141