运行 Azure 上的 powershell 命令 Windows 通过 ARM 模板

Run powershell command on Azure Windows via ARM template

我正在尝试通过创建 VM 的 ARM 模板将数据磁盘装载到 Azure 上的 Windows Vm。这是我的 ARM 资源

{
        "name": "[parameters('virtualMachineName')]",
        "type": "Microsoft.Compute/virtualMachines",
        "apiVersion": "2016-04-30-preview",
        "location": "[parameters('location')]",
        "dependsOn": [
            "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
        ],
        "tags": {
            "Busuness Group": "[parameters('busunessGroup')]",
            "Role": "[parameters('role')]"
        },
        "properties": {
            "osProfile": {
                "computerName": "[parameters('virtualMachineName')]",
                "adminUsername": "[parameters('adminUsername')]",
                "adminPassword": "[parameters('adminPassword')]",
                "windowsConfiguration": {
                    "provisionVmAgent": "true"
                }
            },
            "hardwareProfile": {
                "vmSize": "[parameters('virtualMachineSize')]"
            },
            "storageProfile": {
                "imageReference": {
                    "publisher": "microsoft-ads",
                    "offer": "standard-data-science-vm",
                    "sku": "standard-data-science-vm",
                    "version": "latest"
                },
                "dataDisks": [
                   {
                      "lun": 0,
                      "createOption": "Empty",
                      "caching": "None",
                      "managedDisk": {
                          "storageAccountType": "Premium_LRS"
                      },
                      "diskSizeGB": "[parameters('dataDiskSizeGB')]",
                   }
                ]
            },
            "networkProfile": {
                "networkInterfaces": [
                    {
                        "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
                    }
                ]
            }
        },
        "plan": {
            "name": "standard-data-science-vm",
            "publisher": "microsoft-ads",
            "product": "standard-data-science-vm"
        },
        "resources": [
            {
                "type": "extensions",
                "name": "CustomScriptExtension",
                "apiVersion": "2015-06-15",
                "location": "[resourceGroup().location]",
                "dependsOn": [
                    "[parameters('virtualMachineName')]"
                ],
                "properties": {
                    "publisher": "Microsoft.Compute",
                    "type": "CustomScriptExtension",
                    "typeHandlerVersion": "1.8",
                    "autoUpgradeMinorVersion": true,
                    "settings": {
                      "fileUris": ["https://paste.fedoraproject.org/paste/FMoOq4E3sKoQzqB5Di0DcV5M1UNdIGYhyRLivL9gydE=/raw"]
                    }
                }
            }
        ]
    }

失败并出现以下错误

 {
    "code": "VMExtensionProvisioningError",
    "message": "VM has reported a failure when processing extension 'CustomScriptExtension'. Error message: \"Invalid Configuration - CommandToExecute is not specified in the configuration; it must be specified in either the protected or public configuration section\"."
  }

我也试过直接传递命令

"settings": { 
    "commandToExecute": "Get-Disk |Where partitionstyle -eq ‘raw’ | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel “Data” -Confirm:$false"
    }

两者均无效。我在这里做错了什么?

因此,您需要显式调用 powershell 才能使用 powershell,就像 examples:

"commandToExecute": "[concat('powershell -command ', variable('command'))]"

您可以尝试直接粘贴您的命令,但由于所有引号都无法正确解析,因此请将您的命令保存为变量并像那样连接。