0
votes
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmAdminUsername": {
            "type": "String",
            "metadata": {
                "description": "User name for the Virtual Machine."
            }
        },
        "vmAdminPassword": {
            "type": "SecureString",
            "metadata": {
                "description": "Password for the Virtual Machine."
            }
        },
        "vmName": {
            "type": "String",
            "metadata": {
                "description": "Unique hostname for the Virtual Machine."
            }
        },
        "OSVersion": {
            "defaultValue": "2019-Datacenter",
            "type": "String",
            "metadata": {
                "description": "2019-Datacenter"
            }
        },
        "existingVirtualNetworkResourceGroupName": {
            "type": "String",
            "metadata": {
                "description": "VSTS deployment group name."
            }
        },
        "existingSubnetName": {
            "type": "String",
            "metadata": {
                "description": "Name of the existing subnet in the existing VNET you want to use"
            }
        },
        "existingVirtualNetworkName": {
            "type": "String",
            "metadata": {
                "description": "Name of the existing VNET"
            }
        },
        "vmSize": {
            "defaultValue": "Standard_D2_v3",
            "type": "String",
            "metadata": {
                "description": "Desired Size of the VM. Any valid option accepted but if you choose premium storage type you must choose a DS class VM size."
            }
        },
        "numberOfVms": {
            "defaultValue": "1",
            "type": "Int",
            "metadata": {
                "description": "Give the total number of vm to be deployed."
            }
        },
        "VSTSAccount": {
            "type": "String",
            "metadata": {
                "description": "Specify the name of the VSTSAccount Name"
            }
        },
        "AgentName": {
            "type": "String",
            "metadata": {
                "description": "Specify the name of the Agent"
            }
        },
        "AgentPool": {
            "type": "String",
            "metadata": {
                "description": "Specify the name of the AgentPool which is present"
            }
        },
        "AgentNo": {
            "type": "Int",
            "metadata": {
                "description": "Specify suffix number for Agent name "
            }
        },
        "PATToken": {
            "type": "String",
            "metadata": {
                "description": "Specify the PATToken of organisation or project"
            }
        },
        "resourceTag": {
            "type": "Object",
            "metadata": {
                "description": "Tag of AKS resource."
            }
        }
    },
    "variables": {
        "imagePublisher": "MicrosoftWindowsServer",
        "imageOffer": "WindowsServer",
        "copy": [
            {
                "name": "Agent",
                "count": "[parameters('numberOfVms')]",
                "input": "[concat(parameters('AgentName'), copyIndex('Agent', 1))]"
            }
        ],
        "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',parameters('existingVirtualNetworkName'))]",
        "subnetRef": "[resourceID(parameters('existingVirtualNetworkResourceGroupName'), 'Microsoft.Network/virtualNetWorks/subnets', parameters('existingVirtualNetworkName'), parameters('existingSubnetName'))]"
    },
    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2020-06-01",
            "name": "[concat(parameters('vmName'),'-nic', copyindex(1))]",
            "location": "[resourceGroup().location]",
            "dependsOn": [],
            "tags": "[parameters('resourceTag')]",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            }
                        }
                    }
                ]
            },
            "copy": {
                "name": "nicLoop",
                "count": "[parameters('numberOfVms')]"
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2017-03-30",
            "name": "[concat(parameters('vmName'),'-vm', copyindex(1))]",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/networkInterfaces/', concat(parameters('vmName'),'-nic', copyindex(1)))]"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "osProfile": {
                    "computerName": "[concat(parameters('vmName'),'-vm', copyindex(1))]",
                    "adminUsername": "[parameters('vmAdminUsername')]",
                    "adminPassword": "[parameters('vmAdminPassword')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "[variables('imagePublisher')]",
                        "offer": "[variables('imageOffer')]",
                        "sku": "[parameters('OSVersion')]",
                        "version": "latest"
                    },
                    "osDisk": {
                        "createOption": "FromImage"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces',concat(parameters('vmName'),'-nic', copyindex(1)))]"
                        }
                    ]
                }
            },
            "copy": {
                "name": "vmLoop",
                "count": "[parameters('numberOfVms')]",
                "mode": "serial"
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2018-06-01",
            "name": "[concat('virtualMachineName/agt',copyindex(1))]",
            "location": "[ResourceGroup().location]",
            "dependsOn": [
                "[concat(parameters('vmName'),'-vm', copyindex(1))]"
            ],
            "tags": "[parameters('resourceTag')]",
            "properties": {
                "publisher": "Microsoft.Compute",
                "type": "CustomScriptExtension",
                "autoUpgradeMinorVersion": true,
                "typeHandlerVersion": "1.10",
                "settings": {
                    "fileUris": [
                        "https://ehpiacarmstorage.blob.core.windows.net/armagentscripts/winserviceagt.ps1"
                    ]
                },
                "protectedSettings": {
                    "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\winserviceagt.ps1 -vstsAccount ', parameters('VSTSAccount'), ' -PAT ', parameters('PATToken'), ' -vstsAgent ', variables('Agent'), ' -AgentNo ', parameters('AgentNo'), ' -vmAdminPassword ''', parameters('vmAdminPassword'), ''' -vmAdminUserName ', parameters('vmAdminUsername'), ' -vstsPoolName ', parameters('AgentPool'))]"
                }
            },
            "copy": {
                "name": "agtLoop",
                "count": "[parameters('numberOfVms')]"
            }
        }
    ],
    "outputs": {}
}

Error Message

At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.", "details": [ { "code": "InvalidTemplate", "message": "Unable to process template language expressions for resource '/subscriptions/----------/resourceGroups/mygroup/providers/Microsoft.Compute/virtualMachines/virtualMachineName/extensions/agt2' at line '232' and column '9'. 'The provided parameters for language function 'concat' are invalid. Either all or none of the parameters must be an array. Please see https://aka.ms/arm-template-expressions/#concat for usage details.

1
concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\winserviceagt.ps1 -vstsAccount ' You can check if the returned result is an array after execution. According to the error message, it is because the parameter of array type is included in the ‘concat’ function.Hugh Lin
Yes now it is working fine. after removing the array type in agent name. In script i used %COMPUTER% instead of agent name.Raja Shekar

1 Answers

0
votes
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmAdminUsername": {
            "type": "String",
            "metadata": {
                "description": "User name for the Virtual Machine."
            }
        },
        "vmAdminPassword": {
            "type": "SecureString",
            "metadata": {
                "description": "Password for the Virtual Machine."
            }
        },
        "vmName": {
            "type": "String",
            "metadata": {
                "description": "Unique hostname for the Virtual Machine."
            }
        },
        "OSVersion": {
            "defaultValue": "2019-Datacenter",
            "type": "String",
            "metadata": {
                "description": "2019-Datacenter"
            }
        },
        "indexValue": {
            "defaultValue": "1",
            "type": "Int",
            "metadata": {
                "description": "Give the starting deploy vm number for index start."
            }
        },
        "existingVirtualNetworkResourceGroupName": {
            "type": "String",
            "metadata": {
                "description": "VSTS deployment group name."
            }
        },
        "existingSubnetName": {
            "type": "String",
            "metadata": {
                "description": "Name of the existing subnet in the existing VNET you want to use"
            }
        },
        "existingVirtualNetworkName": {
            "type": "String",
            "metadata": {
                "description": "Name of the existing VNET"
            }
        },
        "vmSize": {
            "defaultValue": "Standard_D2_v3",
            "type": "String",
            "metadata": {
                "description": "Desired Size of the VM. Any valid option accepted but if you choose premium storage type you must choose a DS class VM size."
            }
        },
        "numberOfVms": {
            "defaultValue": "1",
            "type": "Int",
            "metadata": {
                "description": "Give the total number of vm to be deployed."
            }
        },
        "VSTSAccount": {
            "type": "String",
            "metadata": {
                "description": "Specify the name of the VSTSAccount Name"
            }
        },
        "AgentName": {
            "type": "String",
            "metadata": {
                "description": "Specify the name of the Agent"
            }
        },
        "AgentPool": {
            "type": "String",
            "metadata": {
                "description": "Specify the name of the AgentPool which is present"
            }
        },
        "AgentNo": {
            "type": "Int",
            "metadata": {
                "description": "Specify suffix number for Agent name "
            }
        },
        "PATToken": {
            "type": "String",
            "metadata": {
                "description": "Specify the PATToken of organisation or project"
            }
        },
        "resourceTag": {
            "type": "Object",
            "metadata": {
                "description": "Tag of AKS resource."
            }
        }
    },
    "variables": {
        "imagePublisher": "MicrosoftWindowsServer",
        "imageOffer": "WindowsServer",
        "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',parameters('existingVirtualNetworkName'))]",
        "subnetRef": "[resourceID(parameters('existingVirtualNetworkResourceGroupName'), 'Microsoft.Network/virtualNetWorks/subnets', parameters('existingVirtualNetworkName'), parameters('existingSubnetName'))]"
    },
    "resources": [
        {
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2020-06-01",
            "name": "[concat(parameters('vmName'),'-nic', copyindex(parameters('indexValue')))]",
            "location": "[resourceGroup().location]",
            "dependsOn": [],
            "tags": "[parameters('resourceTag')]",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAllocationMethod": "Dynamic",
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            }
                        }
                    }
                ]
            },
            "copy": {
                "name": "nicLoop",
                "count": "[parameters('numberOfVms')]"
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2017-03-30",
            "name": "[concat(parameters('vmName'),'-vm', copyindex(parameters('indexValue')))]",
            "location": "[resourceGroup().location]",
            "tags": "[parameters('resourceTag')]",
            "dependsOn": [
                "[resourceId('Microsoft.Network/networkInterfaces/', concat(parameters('vmName'),'-nic', copyindex(parameters('indexValue'))))]"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('vmSize')]"
                },
                "osProfile": {
                    "computerName": "[concat(parameters('vmName'),'-vm', copyindex(parameters('indexValue')))]",
                    "adminUsername": "[parameters('vmAdminUsername')]",
                    "adminPassword": "[parameters('vmAdminPassword')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "[variables('imagePublisher')]",
                        "offer": "[variables('imageOffer')]",
                        "sku": "[parameters('OSVersion')]",
                        "version": "latest"
                    },
                    "osDisk": {
                        "createOption": "FromImage"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces',concat(parameters('vmName'),'-nic', copyindex(parameters('indexValue'))))]"
                        }
                    ]
                }
            },
            "copy": {
                "name": "vmLoop",
                "count": "[parameters('numberOfVms')]"
            }
        },
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2018-06-01",
            "name": "[concat(parameters('vmName'),'-vm', copyindex(parameters('indexValue')),'/agt')]",
            "location": "[ResourceGroup().location]",
            "dependsOn": [
                "[concat(parameters('vmName'),'-vm', copyindex(parameters('indexValue')))]"
            ],
            "tags": "[parameters('resourceTag')]",
            "properties": {
                "publisher": "Microsoft.Compute",
                "type": "CustomScriptExtension",
                "autoUpgradeMinorVersion": true,
                "typeHandlerVersion": "1.10",
                "settings": {
                    "fileUris": [
                        "https://ehpiacarmstorage.blob.core.windows.net/armagentscripts/winserviceagt.ps1"
                    ]
                },
                "protectedSettings": {
                    "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\winserviceagt.ps1 -vstsAccount ', parameters('VSTSAccount'), ' -PAT ', parameters('PATToken'), ' -vstsAgent ', parameters('AgentName'), ' -AgentNo ', parameters('AgentNo'), ' -vmAdminPassword ''', parameters('vmAdminPassword'), ''' -vmAdminUserName ', parameters('vmAdminUsername'), ' -vstsPoolName ', parameters('AgentPool'))]"
                }
            },
            "copy": {
                "name": "agtLoop",
                "count": "[parameters('numberOfVms')]"
            }
        }
    ],
    "outputs": {}
}