You need to enable JavaScript to run this app. 如何通过ARM模板配置Azure VM扩展执行Git仓库中的PowerShell脚本
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ARM模板配置Azure VM扩展执行Git仓库中的PowerShell脚本

实现方案

你可以通过以下两种方式完成需求,推荐使用第一种方案,适配Azure CustomScriptExtension的原生设计,稳定性更高:

方案1:复用现有fileUris配置(推荐)

Azure自定义脚本扩展会自动下载settings.fileUris中指定的所有文件到脚本执行的工作目录,你只需要做两处修改:

  • 将参数scriptURL的取值设置为你Git仓库中install.ps1的raw直链,例如GitHub仓库的直链格式为https://raw.githubusercontent.com/<用户名>/<仓库名>/<分支名>/<文件层级>/install.ps1
  • 替换protectedSettings.commandToExecute中的本地相对路径为直接的脚本文件名即可

修改后的完整扩展配置如下:

"resources": [
      {
        "type": "Microsoft.Compute/virtualMachines/extensions",
        "name": "[concat(parameters('virtualMachineName'), '/' ,parameters('virtualMachineName'), 'installGW')]",
        "apiVersion": "2019-07-01",
        "location": "[parameters('location')]",
        "tags": {
          "vmname": "[parameters('virtualMachineName')]"
        },
        "properties": {
          "publisher": "Microsoft.Compute",
          "type": "CustomScriptExtension",
          "typeHandlerVersion": "1.7",
          "autoUpgradeMinorVersion": true,
          "settings": {
            "fileUris": [
              "[parameters('scriptURL')]"
            ]
          },
          "protectedSettings": {
            "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -File install.ps1 ', if(equals(parameters('existingDataFactoryVersion'), 'V2'), listAuthKeys(parameters('irId'), '2017-09-01-preview').authKey1, listAuthKeys(parameters('irId'), '2015-10-01').key1))]"
          }
        }
      }
    ]

方案2:直接在命令中调用远程脚本(无需配置fileUris)

如果你不想依赖扩展的预下载能力,可以直接通过PowerShell命令拉取远程脚本并执行,此时可以删除settings.fileUris配置,修改commandToExecute为如下形式:

"commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command \"& {Invoke-Expression (Invoke-WebRequest -Uri ''https://raw.githubusercontent.com/.../install.ps1'' -UseBasicParsing).Content} ''', if(equals(parameters('existingDataFactoryVersion'), 'V2'), listAuthKeys(parameters('irId'), '2017-09-01-preview').authKey1, listAuthKeys(parameters('irId'), '2015-10-01').key1), '''')]"

注意事项

  • 如果你的Git仓库为私有仓库,需要在raw直链中附带个人访问令牌(PAT),如果使用方案1建议将带PAT的链接放在protectedSettings而非settings中,避免明文泄露
  • 调用远程脚本时建议添加-UseBasicParsing参数,避免虚拟机未配置IE引擎导致请求失败

内容的提问来源于stack exchange,提问作者Pirvu Georgian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 02:18:02