Search code examples
azure-resource-managerazure-powershellazure-automationdsc

Azure ARM - DSC VM configuration


I would like to configure my VMs using ARM template and DSC. I prepared simple DCS script in powershell, base on that using powershell command created .zip file. mentioned .zip file uploaded to storage account container. Now I want to use this .zip file to made configuration changes on my test VMs, below my ARM template. I am receiving error message New-AzResourceGroupDeployment : 10:12:09 AM - VM has reported a failure when processing extension 'dscExtension'. Error message: "The DSC Extension failed to execute: Error downloading https://storageAccountName.blob.core.windows.net/containerName/test.zip after 2 attempts: <?xml version="1.0" encoding="utf-8"?><Error><Code>ResourceNotFound</Code><Message>The specified resource does not exist.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "List of virtual machines to be reconfigured, if using multiple VMs, make their names comma separate. E.g. VM01, VM02, VM03."
            },
            "defaultValue": "VM1,VM2"
        },
        "Location": {
            "type": "string",
            "metadata": {
                "description": "Location of the VM"
            },
            "defaultvalue": "WestEurope"
        },
        "functionName": {
            "type": "string",
            "metadata": {
                "description": "Specify the function name"
            },
            "defaultvalue": "test.ps1\\testConfigurationName"
        },
        "storageAccountName": {
            "type": "string",
            "metadata": {
                "description": "Specify the Storage Account name, Storage Account where DCS .zip module is located"
            }
        },
        "setupScriptContainerName": {
            "type": "string",
            "metadata": {
                "description": "Specify the Storage Account container name, container where DCS .zip module is located"
            }
        },
        "DSCSetupArchiveFileName": {
            "type": "string",
            "metadata": {
                "description": "Specify the Storage Account container name, container where DCS .zip module is located"
            },
            "defaultvalue": "test.zip"
        },
        "nodeConfigurationName": {
            "type": "string",
            "metadata": {
                "description": "The name of the node configuration, on the Azure Automation DSC pull server, that this node will be configured as"
            },
            "defaultValue": "testConfigurationName.localhost"
        },
        "registrationKey": {
            "type": "securestring",
            "metadata": {
                "description": "Registration key to use to onboard to the Azure Automation DSC pull/reporting server"
            },
            "defaultValue": "AutomationAccountPrimaryKey"
        },
        "registrationUrl": {
            "type": "string",
            "metadata": {
                "description": "Registration url of the Azure Automation DSC pull/reporting server"
            },
            "defaultValue": AutomationAccountRegistrationURL"
        }
    },
    "variables": {
        "vmListArray": "[split(parameters('vmName'),',')]"
    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2015-06-15",
            "name": "[concat(trim(variables('vmListArray')[copyIndex()]),'/dscExtension')]",
            "copy": {
                "name": "ExtentionLooptoAllVMs",
                "count": "[length(variables('vmListArray'))]"
            },
            "location": "[parameters('Location')]",
            "properties": {
                "autoUpgradeMinorVersion": true,
                "publisher": "Microsoft.Powershell",
                "type": "DSC",
                "typeHandlerVersion": "2.19",
                "protectedSettings": {
                    "Items": {
                        "registrationKeyPrivate": "[parameters('registrationKey')]"
                    }
                },
                "settings": {
                    "ModulesUrl": "[concat('https://',parameters('storageAccountName'),'.blob.core.windows.net/',parameters('setupScriptContainerName'),'/',parameters('DSCSetupArchiveFileName'))]",
                    "ConfigurationFunction": "[parameters('functionName')]",
                    "Properties": [
                        {
                            "Name": "RegistrationKey",
                            "Value": {
                                "UserName": "PLACEHOLDER_DONOTUSE",
                                "Password": "PrivateSettingsRef:registrationKeyPrivate"
                            },
                            "TypeName": "System.Management.Automation.PSCredential"
                        },
                        {
                            "Name": "RegistrationUrl",
                            "Value": "[parameters('registrationUrl')]",
                            "TypeName": "System.String"
                        },
                        {
                            "Name": "NodeConfigurationName",
                            "Value": "[parameters('nodeConfigurationName')]",
                            "TypeName": "System.String"
                        }
                    ]
                }
            }
        }
    ]
}

Updated version:

"resources": [
      {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "apiVersion": "2018-10-01",
          "name": "[concat(trim(variables('vmListArray')[copyIndex()]),'/dscExtension')]",
          "copy": {
              "name": "ExtentionLooptoAllVMs",
              "count": "[length(variables('vmListArray'))]"
          },
          "location": "[parameters('Location')]",
          "properties": {
              "autoUpgradeMinorVersion": true,
              "publisher": "Microsoft.Powershell",
              "type": "DSC",
              "typeHandlerVersion": "2.9",
              "protectedSettings": {
                  "Items": {
                      "registrationKeyPrivate": "[parameters('registrationKey')]"
                  }
              },
              "settings": {
                  "configuration": {
                      "url": "[concat('https://',parameters('storageAccountName'),'.blob.core.windows.net/',parameters('setupScriptContainerName'),'/',parameters('DSCSetupArchiveFileName'))]",
                      "script": "[parameters('scriptName')]",
                      "function": "[parameters('functionName')]"
                  },
                  "Properties": [
                      {
                          "Name": "RegistrationKey",
                          "Value": {
                              "UserName": "PLACEHOLDER_DONOTUSE",
                              "Password": "PrivateSettingsRef:registrationKeyPrivate"
                          },
                          "TypeName": "System.Management.Automation.PSCredential"
                      },
                      {
                          "Name": "RegistrationUrl",
                          "Value": "[parameters('registrationUrl')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "NodeConfigurationName",
                          "Value": "[parameters('nodeConfigurationName')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "ConfigurationMode",
                          "Value": "[parameters('configurationMode')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "ConfigurationModeFrequencyMins",
                          "Value": "[parameters('configurationModeFrequencyMins')]",
                          "TypeName": "System.Int32"
                      },
                      {
                          "Name": "RefreshFrequencyMins",
                          "Value": "[parameters('refreshFrequencyMins')]",
                          "TypeName": "System.Int32"
                      },
                      {
                          "Name": "RebootNodeIfNeeded",
                          "Value": "[parameters('rebootNodeIfNeeded')]",
                          "TypeName": "System.Boolean"
                      },
                      {
                          "Name": "ActionAfterReboot",
                          "Value": "[parameters('actionAfterReboot')]",
                          "TypeName": "System.String"
                      },
                      {
                          "Name": "AllowModuleOverwrite",
                          "Value": "[parameters('allowModuleOverwrite')]",
                          "TypeName": "System.Boolean"
                      }
                  ]
              }
          }
      }
  ]

DSC part:

Configuration SetRegistryxxx {


    Node 'localhost' {
         Registry configxxx {
                Ensure = "Present"
                Key = "HKLM:\xx"
                ValueName = "xx"
                ValueData = "http://0.0.0.0:xxx
                ValueType = "String"
        }
        Registry configxxx {
                Ensure = "Present"
                Key = "HKLM:\xx"
                ValueName = "xx"
                ValueData = "http://0.0.0.0:xx"
                ValueType = "String"
        }
    }
}

Solution

  • According to the error, you can not download the zip file from the Azure blob storage account you use. Please create a sas token for the blob or set the blob access level to Public.

    For example

     "resources": [
        {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "name": "[concat(parameters('vmName'),'/Microsoft.Powershell.DSC')]",
          "apiVersion": "2015-06-15",
          "location": "[parameters('location')]",
          "properties": {
            "publisher": "Microsoft.Powershell",
            "type": "DSC",
            "typeHandlerVersion": "2.19",
            "autoUpgradeMinorVersion": true,
            "protectedSettings": {
              "Items": {
                "registrationKeyPrivate": "[parameters('registrationKey')]"
              }
            },
            "settings": {
              "ModulesUrl": "<the url of you azure blob>",
              "SasToken": "<the sas token for the blob>",
              "ConfigurationFunction": "[parameters('configurationFunction')]",
            ...
    }
    ]
    

    For more details, please refer to the document and the template