代码之家  ›  专栏  ›  技术社区  ›  Mark Allison

如何使用ARM模板在Azure VM部署期间运行PowerShell脚本?

  •  0
  • Mark Allison  · 技术社区  · 6 年前

    我可以这样做: 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 将脚本作为部署的一部分上载到azure存储位置。

    2 回复  |  直到 6 年前
        1
  •  1
  •   4c74356b41    6 年前

    鸡\蛋问题。您无法使用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"
                }
            }
        }
    },
    
        2
  •  0
  •   bmoore-msft    6 年前

    您的意思是如何在quickstart示例中设置_artifactsLocation?如果是这样,您有2个选项(或3个,具体取决于)

    1) 使用QS repo中的脚本,_artifactsLocation参数的默认值将为您设置该值。。。

    3) 将PS1放置在您自己的某个位置,并手动设置_artifactsLocation和_artifactsLocationSasToken的值

    您也可以从gallery.azure.com进行部署,但这将迫使您使用galley中存储的脚本(与使用GitHub中的默认值相同)

    那有帮助吗?