【问题标题】:How to dynamically loop through Key Value Pairs in powershell/azure cli如何在powershell / azure cli中动态循环键值对
【发布时间】:2020-03-08 14:58:03
【问题描述】:

我想更改 azure webapp 设置,我可以通过以下 powershell 代码来实现。如您所见,有变量及其值,我想动态调用变量及其值并循环遍历它,并且只使用该命令一次。有没有办法实现它

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"

【问题讨论】:

  • 请允许我给你一个标准的建议给新手:如果一个答案解决了你的问题,请accept it点击它旁边的大复选标记(✓),也可以选择投票(投票至少需要 15 个声望点)。如果您发现其他答案有帮助,请给他们投票。接受(您将获得 2 个声望点)和投票可以帮助未来的读者。如果您的问题尚未得到完全解答,请提供反馈或self-answer

标签: azure powershell cloud azure-cli


【解决方案1】:
  • az CLI 接受 多个 键值对作为其 --settings 参数的参数。

  • PowerShell 允许您将数组元素作为单独的参数传递给外部程序(例如 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

警告:CLI az 实现为 az.cmd,即一个批处理文件(调用 Python),因此传递给它的参数可能需要 转义以满足cmd.exe的语法要求。

【讨论】:

    【解决方案2】:

    使用 powershell + az cli 的最简单方法:

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

    你也可以在没有 powershell 的情况下使用 xargs 实现同样的效果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2011-01-23
      • 1970-01-01
      相关资源
      最近更新 更多