【问题标题】:Azure ARM template deployment via CLI failing with .ps1 script extension error通过 CLI 部署 Azure ARM 模板失败并出现 .ps1 脚本扩展错误
【发布时间】:2017-09-26 09:28:52
【问题描述】:

我一直在通过带有自定义脚本扩展名 (.ps1) 的 Visual Studio 部署我的 ARM 模板,它实际上也是通过 VS 编译的。但是,我一直在尝试通过 CLI 部署它,并对 Azure 存储位置上的 .ps1 文件进行修改,以便其他没有 VS 的人可以部署它。但是,每次我进行部署时,它都会失败,脚本没有 .ps1 扩展名的错误。

我的 ARM 模板的自定义脚本扩展部分:

      "name": "formatDataDisk",
      "type": "extensions",
      "location": "[resourceGroup().location]",
      "apiVersion": "2016-03-30",
      "dependsOn": [
        "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]"
      ],
      "tags": {
        "displayName": "formatDataDisk"
      },
      "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.4",
        "autoUpgradeMinorVersion": true,
        "settings": {
          "fileUris": [
            "https://--MYSTORAGEACCOUNTNAME--.blob.core.windows.net/customscript/formatDataDisk.ps1"
          ],
          "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File './customscript/formatDatadisk1.ps1'"
        },
        "protectedSettings": {
          "storageAccountName": "--MYSTORAGEACCOUNTNAME--",
          "storageAccountKey": "--MYSTORAGEKEY--"
        }

在 CLI 部署结束时失败:

msrest.http_logger : b'{"status":"Failed","error":{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.","details":[{"code":"Conflict","message":"{\\r\\n  \\"status\\": \\"Failed\\",\\r\\n  \\"error\\": {\\r\\n    \\"code\\": \\"ResourceDeploymentFailure\\",\\r\\n    \\"message\\": \\"The resource operation completed with terminal provisioning state \'Failed\'.\\",\\r\\n    \\"details\\": [\\r\\n      {\\r\\n        \\"code\\": \\"VMExtensionProvisioningError\\",\\r\\n        \\"message\\": \\"VM has reported a failure when processing extension \'formatDataDisk\'. Error message: \\\\\\"Finished executing command\\\\\\".\\"\\r\\n      }\\r\\n    ]\\r\\n  }\\r\\n}"}]}}'
    msrest.exceptions : At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
    Deployment failed. {
      "status": "Failed",
      "error": {
        "code": "ResourceDeploymentFailure",
        "message": "The resource operation completed with terminal provisioning state 'Failed'.",
        "details": [
          {
            "code": "VMExtensionProvisioningError",
            "message": "VM has reported a failure when processing extension 'formatDataDisk'. Error message: \"Finished executing command\"."
          }

Azure 门户在自定义脚本扩展中显示此错误:

[
    {
        "code": "ComponentStatus/StdOut/succeeded",
        "level": "Info",
        "displayStatus": "Provisioning succeeded",
        "message": ""
    },
    {
        "code": "ComponentStatus/StdErr/succeeded",
        "level": "Info",
        "displayStatus": "Provisioning succeeded",
        "message": "Processing -File ''./customscript/formatDatadisk1.ps1'' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again."
    }
]

我试过了:

任何指导将不胜感激

【问题讨论】:

    标签: json powershell azure templates command-line-interface


    【解决方案1】:

    最后,我在其他 cmets 的帮助下解决了这个问题。我用一个更简单的 helloworld.ps1 脚本进行了测试,它可以工作,所以很明显我的 powershell 脚本存在问题。在 Visual Studio 中创建它时,它会将以下内容放在顶部:

    <# Custom Script for Windows #>
    

    接下来是脚本的其余部分。一旦我删除了它并重新上传到我的 StorageAccount/Container,并在 commandToExecute 中删除了 ./,就很好了。

    感谢您的反馈。

    【讨论】:

    • 我在我的实验室测试,我认为根本原因是你应该在命令中删除' 'powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File ./customscript/formatDatadisk1.ps1
    【解决方案2】:

    路径错误,需要省略路径中的容器:

    "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File './formatDatadisk1.ps1'"
    

    它总是把下载的文件放在同一个目录下,没有嵌套文件夹

    这是适合我的确切 sn-p:

    "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.8",
        "autoUpgradeMinorVersion": true,
        "settings": {
            "fileUris": [
                "https://backupcicd.blob.core.windows.net/deployment/iis-preConfigureScript.ps1"
            ],
            "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File iis-preConfigureScript.ps1"
        }
    }
    

    【讨论】:

    • 遗憾的是这没有任何区别,它仍然失败并出现相同的错误。
    • 我仍然在这里挣扎。我像你一样设置我的代码,但它仍然失败,刚才我将 autoUpgradeMinorVersion 设置为 false,希望可能版本不喜欢它,但它又失败了。
    • 嗯,这是我的生产代码,所以它可以工作,你可以直接使用这块,它会工作 :) 只需将你的脚本上传到某个地方,更改 fileUris 指向那个地方然后你很好,很明显改变传递给-File的参数
    • 你能告诉我你在这个脚本上运行的是什么 apiVersion 吗?
    • "2016-04-30-preview"
    【解决方案3】:

    当我在实验室测试您的模板时,我会收到与您相同的错误日志。当我删除 ./customscript/ 时,它对我有用。

      "resources": [
        {
          "name": "[concat(parameters('virtualMachineName'), '/', 'shuitest')]",
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "location": "eastus",
          "apiVersion": "2016-03-30",
          "dependsOn": [ ],
          "tags": {
            "displayName": "shuitest"
          },
          "properties": {
            "publisher": "Microsoft.Compute",
            "type": "CustomScriptExtension",
            "typeHandlerVersion": "1.4",
            "autoUpgradeMinorVersion": true,
            "settings": {
              "fileUris": ["https://shuiwindisks928.blob.core.windows.net/vhds/open_port.ps1"]
    
            },
            "protectedSettings": {
              "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File open_port.ps1",
              "storageAccountName" : "shuiwindisks928",
              "storageAccountKey" : "jvYg+1aCo+d4b+FI/kdBOtE*******************+kXa0yZR5xrt7a57qgHw=="
            }
          }
        }],
    

    更新:

    你需要检查VM上的扩展日志,请参考这个link

    【讨论】:

    • @Beefcake 如果我使用"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File './formatDatadisk1.ps1'",您可以尝试删除./。我相信它会起作用的。
    • 我之前在没有./ 的情况下尝试过,但没有成功。不幸的是,我今天没有机会再次部署脚本,但明天早上我会再试一次。
    • @Beefcake 很好,你也可以检查登录虚拟机。 docs.microsoft.com/en-us/azure/virtual-machines/windows/…
    • 因为这不是“解决”问题的原因。建议很有帮助,但它并没有解决我的问题的根本原因,改变了 .ps1 文件。
    • @Beefcake 您好,感谢您的回复。如果您分享您的脚本,也许我们可以更快地解决您的问题。但我很高兴听到您解决了您的问题。
    【解决方案4】:

    我不确定它是否与此特定问题有关,但我在过去 3-4 天使用 Azure 自定义扩展时注意到的是,如果您的脚本失败一次并且您的自定义扩展的 ARM 模板部署失败,那么您将不得不从尝试运行脚本的 Azure 门户的扩展刀片下的 VM 中手动删除自定义扩展。如果您不这样做,无论您对脚本进行了何种更改,自定义扩展都将继续运行旧脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      相关资源
      最近更新 更多