I have a powershell script to install a VSTS Build Agent in an ARM template. This template is based off the azure quickstart templates here.
I want to use the "copy" function to run the script multiple times because I want to install 10 agents when my VM is deployed. When I try to deploy my template I get this error:
Error: Code=InvalidTemplate; Message=Deployment template validation failed: 
'The template resource 'CustomScript' at line '247' column '13' is not valid. Copying nested resources is not supported.
My question is, how can I install 10 build agents with the copy function so that I have vsts-agent-1, vsts-agent-2, etc?
Here's the relevant snippet of the template:
{
      "name": "[parameters('vmName')]",
      "type": "Microsoft.Compute/virtualMachines",
      "location": "[parameters('location')]",
      "apiVersion": "2017-03-30",
      "dependsOn": [
        "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[concat('Microsoft.Network/networkInterfaces/', variables('vmNicName'))]"
      ],
      "tags": {
        "displayName": "VM01"
      },
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[parameters('vmName')]",
          "adminUsername": "[parameters('vmAdminUserName')]",
          "adminPassword": "[parameters('vmAdminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('vmImagePublisher')]",
            "offer": "[variables('vmImageOffer')]",
            "sku": "[parameters('vmVisualStudioVersion')]",
            "version": "latest"
          },
          "osDisk": {
            "name": "[concat(parameters('vmName'),'_OSDisk')]",
            "caching": "ReadWrite",
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('vmNicName'))]"
            }
          ]
        }
      },
      "resources": [
        {
          "name": "CustomScript",
          "type": "extensions",
          "location": "[parameters('location')]",
          "apiVersion": "2015-05-01-preview",
          "dependsOn": [
            "[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"
          ],
          "properties": {
            "publisher": "Microsoft.Compute",
            "type": "CustomScriptExtension",
            "typeHandlerVersion": "1.4",
            "settings": {
              "fileUris": [
                "[concat(parameters('_artifactsLocation'),'/InstallVSTSAgent.ps1')]"
              ],
              "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\InstallVSTSAgent.ps1 -vstsAccount ', parameters('vstsAccount'), ' -personalAccessToken ', parameters('personalAccessToken'), ' -AgentName ', parameters('vstsAccount'), ' -PoolName ', parameters('poolName'), ' -runAsAutoLogon ', parameters('enableAutologon'), ' -vmAdminUserName ', parameters('vmAdminUserName'), ' -vmAdminPassword ', parameters('vmAdminPassword'))]"
            }
          }
        }
      ]
EDIT1
I've updated the template and moved the child resource out so that the child resource is at the same level as the parent. This section now looks like this:
{
  "name": "CustomScript",
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "location": "[parameters('location')]",
  "apiVersion": "2015-05-01-preview",
  "dependsOn": [
    "[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"
  ],
  "copy": {
    "name": "customScriptGroup",
    "count": "[parameters('agentCount')]"
  },
  "properties": {
    "publisher": "Microsoft.Compute",
    "type": "CustomScriptExtension",
    "typeHandlerVersion": "1.4",
    "protectedSettings": {
      "fileUris": [
        "[concat(parameters('_artifactsLocation'),'/InstallVSTSAgent.ps1')]"
      ],
      "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\InstallVSTSAgent.ps1 -vstsAccount ', parameters('vstsAccount'), ' -personalAccessToken ', parameters('personalAccessToken'), ' -AgentName ', parameters('vstsAccount')[copyIndex(1)], ' -PoolName ', parameters('poolName'), ' -runAsAutoLogon ', parameters('enableAutologon'), ' -vmAdminUserName ', parameters('vmAdminUserName'), ' -vmAdminPassword ', parameters('vmAdminPassword'))]"
    }
  }
}
However, when I try and deploy I get this error:
Error: Code=InvalidTemplate; Message=Deployment template validation failed:
The template resource 'CustomScript' for type 'Microsoft.Compute/virtualMachines/extensions' at line '247' and column '9' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name.
 
     
    