首页 > 解决方案 > 如何在使用 ARM 模板的 Azure VM 部署期间运行 PowerShell 脚本?

问题描述

我想使用 Azure 资源管理器 (ARM) 在 Azure 中部署 VM,然后在 VM 部署后运行 PowerShell 脚本来配置它。

我可以用这样的方法做到这一点:https ://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-vsts-agent

但是,该模板从 GitHub 获取 PowerShell 脚本。作为部署的一部分,我想将脚本上传到 Azure 存储,然后让 VM 从 Azure 存储中获取脚本并运行它。关于 PowerShell 脚本的依赖关系,我该如何做这部分,因为它必须存在于 Azure 存储中的某个位置,然后才能执行。

我目前有这个来安装 VSTS 代理作为部署的一部分,但是脚本是从 GitHub 下载的,我不想这样做,我希望 VSTS 代理的安装脚本成为我的 ARM 项目的一部分。

{
          "name": "vsts-build-agents",
          "type": "extensions",
          "location": "[parameters('location')]",
          "apiVersion": "2017-12-01",
          "dependsOn": [
            "vsts-build-vm"
          ],
          "tags": {
            "displayName": "VstsInstallScript"
          },
          "properties": {
            "publisher": "Microsoft.Compute",
            "type": "CustomScriptExtension",
            "typeHandlerVersion": "1.9",
            "settings": {
              "fileUris": [
                "[concat(parameters('_artifactsLocation'), '/', variables('powerShell').folder, '/', variables('powerShell').script, parameters('_artifactsLocationSasToken'))]"
              ]
            },
            "protectedSettings": {
              "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command \"& {', './', variables('powerShell').script, ' ', variables('powerShell').buildParameters, '}\"')]"
            }
          }
        }

我想我的问题实际上是关于如何设置_azurestoragelocation到脚本刚刚作为部署的一部分上传的天蓝色存储位置。

标签: powershellazureazure-resource-managerarm-template

解决方案


鸡\蛋问题。您无法使用 arm 模板上传到 azure 存储,您需要使用脚本上传到 azure 存储,但是如果您在 vm 上有该脚本来上传它,您实际上不需要上传它。

话虽如此,你为什么不使用 VSTS 代理扩展?

{
    "name": "xxx",
    "apiVersion": "2015-01-01",
    "type": "Microsoft.Resources/deployments",
    "properties": {
        "mode": "Incremental",
        "templateLink": {
            "uri": "https://gallery.azure.com/artifact/20161101/microsoft.vsts-agent-windows-arm.1.0.0/Artifacts/MainTemplate.json"
        },
        "parameters": {
            "vmName": {
                "value": "xxx"
            },
            "location": {
                "value": "xxx"
            },
            "VSTSAccountName": {
                "value": "xxx"
            },
            "TeamProject": {
                "value": "xxx"
            },
            "DeploymentGroup": {
                "value": "Default"
            },
            "AgentName": {
                "value": "xxx"
            },
            "PATToken": {
                "value": "xxx"
            }
        }
    }
},

推荐阅读