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

时间:2017-03-04 20:01:03

标签: windows powershell azure azure-resource-manager

我正在尝试通过创建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"
    }
两个都没有工作。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

因此,您需要显式调用powershell以使用powershell,就像在examples中一样:

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

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