Search code examples
azurepowershellazure-vm-scale-set

Azure VM Scale Set CustomScriptExtension Failing


I'm trying to create a windows Azure VM Scale Set that auto provisions a formatted attached data disk using the MS guide here: https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-attached-disks

No matter what I do I see to get this error:

9:14:16 PM - The deployment 'testvmss' failed with error(s). Showing 1 out of 1 error(s). Status | Message: VM has reported a failure when processing extension 'customScript'. Error message: "Invalid | Configuration - CommandToExecute is not specified in the configuration; it must be specified in either | the protected or public configuration section" More information on troubleshooting is available at | https://aka.ms/VMExtensionCSEWindowsTroubleshoot (Code:VMExtensionProvisioningError) CorrelationId: | 3294f49a-23f0-4634-aba0-3bb0e814659e

I've tried:

  • moving the "commandToExecute" into the protected config area
  • "typeHandlerVersion" numbers
  • moving commandToExecute outside of the "settings" section
  • using case corrected "CommandToExecute"
  • Simplifying the powershell statement to "powershell echo test"
  • Searching google for the error message, better/different examples, etc.

Here is the specific section of the ARM:

"extensionProfile": {
            "extensions": [
              {
                "name": "customScript",
                "properties": {
                  "publisher": "Microsoft.Compute",
                  "type": "CustomScriptExtension",
                  "typeHandlerVersion": "1.8",
                  "autoUpgradeMinorVersion": true,
                  "settings": {
                    "fileUris": ["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.ps1"],
                    "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
                  }
                }
              }
            ]
          }

And for reference, here is the full ARM:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string"
    },
    "virtualMachineScaleSetName": {
      "type": "string"
    },
    "virtualMachineScaleSetRG": {
      "type": "string"
    },
    "singlePlacementGroup": {
      "type": "string"
    },
    "instanceSize": {
      "type": "string"
    },
    "instanceCount": {
      "type": "string"
    },
    "upgradeMode": {
      "type": "string"
    },
    "priority": {
      "type": "string"
    },
    "enableAcceleratedNetworking": {
      "type": "string"
    },
    "subnetId": {
      "type": "string"
    },
    "osDiskType": {
      "type": "string"
    },
    "dataDisks": {
      "type": "array"
    },
    "addressPrefixes": {
      "type": "array"
    },
    "subnets": {
      "type": "array"
    },
    "virtualNetworkId": {
      "type": "string"
    },
    "virtualNetworkName": {
      "type": "string"
    },
    "networkSecurityGroups": {
      "type": "array"
    },
    "networkInterfaceConfigurations": {
      "type": "array"
    },
    "vmName": {
      "type": "string"
    },
    "scaleInPolicy": {
      "type": "object"
    },
    "overprovision": {
      "type": "bool"
    },
    "upgradePolicy": {
      "type": "string"
    },
    "adminUsername": {
      "type": "string"
    },
    "adminPassword": {
      "type": "secureString"
    },
    "platformFaultDomainCount": {
      "type": "string"
    }
  },
  "variables": {
    "storageApiVersion": "2019-04-01",
    "namingInfix": "[toLower(substring(concat(parameters('virtualMachineScaleSetName'), uniqueString(resourceGroup().id)), 0, 9))]"
  },
  "resources": [
    {
      "name": "[parameters('virtualNetworkName')]",
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2019-09-01",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": "[parameters('addressPrefixes')]"
        },
        "subnets": "[parameters('subnets')]"
      }
    },
    {
      "name": "[parameters('networkSecurityGroups')[copyIndex()].name]",
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2019-02-01",
      "location": "[parameters('location')]",
      "properties": {
        "securityRules": "[parameters('networkSecurityGroups')[copyIndex()].rules]"
      },
      "copy": {
        "name": "networkSecurityGroups",
        "count": "[length(parameters('networkSecurityGroups'))]"
      }
    },
    {
      "name": "[parameters('virtualMachineScaleSetName')]",
      "type": "Microsoft.Compute/virtualMachineScaleSets",
      "apiVersion": "2019-12-01",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
        "networkSecurityGroups",
        "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]"
      ],
      "sku": {
        "name": "[parameters('instanceSize')]",
        "capacity": "[int(parameters('instanceCount'))]"
      },
      "properties": {
        "overprovision": "[parameters('overprovision')]",
        "upgradePolicy": {
          "mode": "[parameters('upgradePolicy')]"
        },
        "singlePlacementGroup": "[parameters('singlePlacementGroup')]",
        "virtualMachineProfile": {
          "storageProfile": {
            "osDisk": {
              "createOption": "fromImage",
              "caching": "ReadWrite",
              "managedDisk": {
                "storageAccountType": "[parameters('osDiskType')]"
              }
            },
            "imageReference": {
              "publisher": "MicrosoftWindowsServer",
              "offer": "WindowsServer",
              "sku": "2016-Datacenter",
              "version": "latest"
            },
            "copy": [
              {
                "name": "dataDisks",
                "count": "[length(parameters('dataDisks'))]",
                "input": {
                  "lun": "[parameters('dataDisks')[copyIndex('dataDisks')].lun]",
                  "createOption": "[parameters('dataDisks')[copyIndex('dataDisks')].createOption]",
                  "caching": "[parameters('dataDisks')[copyIndex('dataDisks')].caching]",
                  "writeAcceleratorEnabled": "[parameters('dataDisks')[copyIndex('dataDisks')].writeAcceleratorEnabled]",
                  "diskSizeGB": "[parameters('dataDisks')[copyIndex('dataDisks')].diskSizeGB]",
                  "managedDisk": {
                    "storageAccountType": "[parameters('dataDisks')[copyIndex('dataDisks')].storageAccountType]",
                    "diskEncryptionSet": "[parameters('dataDisks')[copyIndex('dataDisks')].diskEncryptionSet]"
                  },
                  "diskIOPSReadWrite": "[if(equals( parameters('dataDisks')[copyIndex('dataDisks')].diskIOPSReadWrite, -1), json('null'),parameters('dataDisks')[copyIndex('dataDisks')].diskIOPSReadWrite)]",
                  "diskMBpsReadWrite": "[if(equals( parameters('dataDisks')[copyIndex('dataDisks')].diskMBpsReadWrite, -1), json('null'),parameters('dataDisks')[copyIndex('dataDisks')].diskMBpsReadWrite)]"
                }
              }
            ]
          },
          "priority": "[parameters('priority')]",
          "networkProfile": {
            "copy": [
              {
                "name": "networkInterfaceConfigurations",
                "count": "[length(parameters('networkInterfaceConfigurations'))]",
                "input": {
                  "name": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].name]",
                  "properties": {
                    "primary": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].primary]",
                    "enableAcceleratedNetworking": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].enableAcceleratedNetworking]",
                    "ipConfigurations": [
                      {
                        "name": "[concat(parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].name, '-defaultIpConfiguration')]",
                        "properties": {
                          "subnet": {
                            "id": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].subnetId]"
                          },
                          "primary": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].primary]",
                          "applicationGatewayBackendAddressPools": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].applicationGatewayBackendAddressPools]",
                          "loadBalancerBackendAddressPools": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].loadBalancerBackendAddressPools]",
                          "loadBalancerInboundNatPools": "[parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].loadBalancerInboundNatPools]",
                          "publicIPAddressConfiguration": "[if( equals( parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].pipName, ''), json('null'), union(json(concat('{\"name\": \"', parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].pipName, '\"}'))\n                        ,json('{\"properties\": { \"idleTimeoutInMinutes\": 15}}')))]"
                        }
                      }
                    ],
                    "networkSecurityGroup": "[if( equals( parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].nsgId, ''), json('null'),json(concat('{\"id\": \"', parameters('networkInterfaceConfigurations')[copyIndex('networkInterfaceConfigurations')].nsgId, '\"}')))]"
                  }
                }
              }
            ]
          },
          "extensionProfile": {
            "extensions": [
              {
                "name": "customScript",
                "properties": {
                  "publisher": "Microsoft.Compute",
                  "type": "CustomScriptExtension",
                  "typeHandlerVersion": "1.8",
                  "autoUpgradeMinorVersion": true,
                  "settings": {
                    "fileUris": ["https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.ps1"],
                    "commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
                  }
                }
              }
            ]
          },
          "osProfile": {
            "computerNamePrefix": "[variables('namingInfix')]",
            "adminUsername": "[parameters('adminUsername')]",
            "adminPassword": "[parameters('adminPassword')]",
            "windowsConfiguration": {
              "provisionVmAgent": true
            }
          }
        },
        "scaleInPolicy": "[parameters('scaleInPolicy')]",
        "platformFaultDomainCount": "[parameters('platformFaultDomainCount')]"
      }
    }
  ],
  "outputs": {
    "adminUsername": {
      "type": "string",
      "value": "[parameters('adminUsername')]"
    }
  }
}

Any help is greatly appreciated, thanks!


Solution

  • As the MS guide template, you need to include a dataDisks section in the storageProfile of the Microsoft.Compute/virtualMachineScaleSets resource(s) and deploy the template.

    Here is a working template for your references:

    {
        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "vmSku": {
                "type": "string",
                "defaultValue": "Standard_A1_v2",
                "metadata": {
                    "description": "Size of VMs in the VM Scale Set."
                }
            },
            "windowsOSVersion": {
                "type": "string",
                "defaultValue": "2016-Datacenter"
            },
            "vmssName": {
                "type": "string",
                "minLength": 3,
                "maxLength": 61
            },
            "instanceCount": {
                "type": "int",
                "defaultValue": 3,
                "minValue": 1,
                "maxValue": 100,
                "metadata": {
                    "description": "Number of VM instances (100 or less)."
                }
            },
            "singlePlacementGroup": {
                "type": "bool",
                "defaultValue": true
            },
            "adminUsername": {
                "type": "string",
                "defaultValue": "vmssadmin"
            },
            "adminPassword": {
                "type": "securestring"
            },
            "location": {
                "type": "string",
                "defaultValue": "[resourceGroup().location]"
            },
            "platformFaultDomainCount": {
                "type": "int",
                "defaultValue": 1
            }
        },
        "variables": {
            "namingInfix": "[toLower(substring(concat(parameters('vmssName'), uniqueString(resourceGroup().id)), 0, 9))]",
            "longNamingInfix": "[toLower(parameters('vmssName'))]",
            "addressPrefix": "10.0.0.0/16",
            "subnetPrefix": "10.0.0.0/24",
            "virtualNetworkName": "[concat(variables('namingInfix'), 'vnet')]",
            "publicIPAddressName": "[concat(variables('namingInfix'), 'pip')]",
            "subnetName": "[concat(variables('namingInfix'), 'subnet')]",
            "loadBalancerName": "[concat(variables('namingInfix'), 'lb')]",
            "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
            "lbProbeID": "[resourceId('Microsoft.Network/loadBalancers/probes',variables('loadBalancerName'), 'tcpProbe')]",
            "natPoolName": "[concat(variables('namingInfix'), 'natpool')]",
            "bePoolName": "[concat(variables('namingInfix'), 'bepool')]",
            "lbPoolID": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools',variables('loadBalancerName'),variables('bePoolName'))]",
            "natStartPort": 50000,
            "natEndPort": 50119,
            "natBackendPort": 3389,
            "nicName": "[concat(variables('namingInfix'), 'nic')]",
            "ipConfigName": "[concat(variables('namingInfix'), 'ipconfig')]",
            "frontEndIPConfigID": "[resourceId('Microsoft.Network/loadBalancers/frontendIPConfigurations',variables('loadBalancerName'),'loadBalancerFrontEnd')]",
            "osType": {
                "publisher": "MicrosoftWindowsServer",
                "offer": "WindowsServer",
                "sku": "[parameters('windowsOSVersion')]",
                "version": "latest"
            },
            "imageReference": "[variables('osType')]"
        },
        "resources": [
            {
                "type": "Microsoft.Network/loadBalancers",
                "apiVersion": "2020-06-01",
                "name": "[variables('loadBalancerName')]",
                "location": "[parameters('location')]",
                "dependsOn": [
                    "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
                ],
                "properties": {
                    "frontendIPConfigurations": [
                        {
                            "name": "LoadBalancerFrontEnd",
                            "properties": {
                                "publicIPAddress": {
                                    "id": "[variables('publicIPAddressID')]"
                                }
                            }
                        }
                    ],
                    "backendAddressPools": [
                        {
                            "name": "[variables('bePoolName')]"
                        }
                    ],
                    "inboundNatPools": [
                        {
                            "name": "[variables('natPoolName')]",
                            "properties": {
                                "frontendIPConfiguration": {
                                    "id": "[variables('frontEndIPConfigID')]"
                                },
                                "protocol": "Tcp",
                                "frontendPortRangeStart": "[variables('natStartPort')]",
                                "frontendPortRangeEnd": "[variables('natEndPort')]",
                                "backendPort": "[variables('natBackendPort')]"
                            }
                        }
                    ],
                    "loadBalancingRules": [
                        {
                            "name": "LBRule",
                            "properties": {
                                "frontendIPConfiguration": {
                                    "id": "[variables('frontEndIPConfigID')]"
                                },
                                "backendAddressPool": {
                                    "id": "[variables('lbPoolID')]"
                                },
                                "protocol": "Tcp",
                                "frontendPort": 80,
                                "backendPort": 80,
                                "enableFloatingIP": false,
                                "idleTimeoutInMinutes": 5,
                                "probe": {
                                    "id": "[variables('lbProbeID')]"
                                }
                            }
                        }
                    ],
                    "probes": [
                        {
                            "name": "tcpProbe",
                            "properties": {
                                "protocol": "Tcp",
                                "port": 80,
                                "intervalInSeconds": 5,
                                "numberOfProbes": 2
                            }
                        }
                    ]
                }
            },
            {
                "type": "Microsoft.Compute/virtualMachineScaleSets",
                "apiVersion": "2020-06-01",
                "name": "[variables('namingInfix')]",
                "location": "[parameters('location')]",
                "sku": {
                    "name": "[parameters('vmSku')]",
                    "tier": "Standard",
                    "capacity": "[parameters('instanceCount')]"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Network/loadBalancers', variables('loadBalancerName'))]",
                    "[resourceId('Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]"
                ],
                "properties": {
                    "overprovision": true,
                    "upgradePolicy": {
                        "mode": "Automatic"
                    },
                    "singlePlacementGroup": "[parameters('singlePlacementGroup')]",
                    "platformFaultDomainCount": "[parameters('platformFaultDomainCount')]",
                    "virtualMachineProfile": {
                        "storageProfile": {
                            "osDisk": {
                                "caching": "ReadWrite",
                                "createOption": "FromImage"
                            },
                            "dataDisks": [
                                {
                                "diskSizeGB": 128,
                                "lun": 0,
                                "createOption": "Empty"
                                }
                            ],
                            "imageReference": "[variables('imageReference')]"
                        },
                        "osProfile": {
                            "computerNamePrefix": "[variables('namingInfix')]",
                            "adminUsername": "[parameters('adminUsername')]",
                            "adminPassword": "[parameters('adminPassword')]"
                        },
                        "networkProfile": {
                            "networkInterfaceConfigurations": [
                                {
                                    "name": "[variables('nicName')]",
                                    "properties": {
                                        "primary": true,
                                        "ipConfigurations": [
                                            {
                                                "name": "[variables('ipConfigName')]",
                                                "properties": {
                                                    "subnet": {
                                                        "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]"
                                                    },
                                                    "loadBalancerBackendAddressPools": [
                                                        {
                                                            "id": "[variables('lbPoolID')]"
                                                        }
                                                    ],
                                                    "loadBalancerInboundNatPools": [
                                                        {
                                                            "id": "[resourceId('Microsoft.Network/loadBalancers/inboundNatPools', variables('loadBalancerName'),  variables('natPoolName'))]"
                                                        }
                                                    ]
                                                }
                                            }
                                        ]
                                    }
                                }
                            ]
                        },
                        "extensionProfile": {
                            "extensions": [
    
    {
        "name": "customScript",
        "properties": {
            "publisher": "Microsoft.Compute",
            "type": "CustomScriptExtension",
            "typeHandlerVersion": "1.8",
            "autoUpgradeMinorVersion": true,
            "settings": {
            "fileUris": [
                "https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/prepare_vm_disks.ps1"
            ],
            "commandToExecute": "powershell -ExecutionPolicy Unrestricted -File prepare_vm_disks.ps1"
            }
        }
    }
    
    
                            ]
                        }
                    }
                }
            },
            {
                "type": "Microsoft.Network/publicIPAddresses",
                "apiVersion": "2020-06-01",
                "name": "[variables('publicIPAddressName')]",
                "location": "[parameters('location')]",
                "properties": {
                    "publicIPAllocationMethod": "Static",
                    "dnsSettings": {
                        "domainNameLabel": "[variables('longNamingInfix')]"
                    }
                }
            },
            {
                "type": "Microsoft.Network/virtualNetworks",
                "apiVersion": "2020-06-01",
                "name": "[variables('virtualNetworkName')]",
                "location": "[parameters('location')]",
                "properties": {
                    "addressSpace": {
                        "addressPrefixes": [
                            "[variables('addressPrefix')]"
                        ]
                    },
                    "subnets": [
                        {
                            "name": "[variables('subnetName')]",
                            "properties": {
                                "addressPrefix": "[variables('subnetPrefix')]"
                            }
                        }
                    ]
                }
            }
          
        ],
        "outputs": {}
    }
    

    If you want to create multiple data disks with the copy function, you can add it like this

                "storageProfile": {
                    "osDisk": {
                        "caching": "ReadWrite",
                        "createOption": "FromImage"
                    },
      "copy": [
        {
          "name": "dataDisks",
          "count": "[parameters('numberOfDataDisks')]",
          "input": {
            "diskSizeGB": 1023,
            "lun": "[copyIndex('dataDisks')]",
            "createOption": "Empty"
          }
        }
      ],
    

    After my validation, after the above deployment finishs, you need to initialize the disk in each instance, then you could get the expected result.

    enter image description here

    Reference: https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/tutorial-use-disks-powershell#prepare-the-data-disks