Search code examples
azurepowershellazure-resource-managerazure-rm-template

ARM Template: how to read output result in array format


I posted some question on stackoverflow and would like to thanks those who take time to answer. Although it exists documentation, i still face an issue related to output feature.

Basically I understand how to retrieve data when format is string. Unfortunately when data is on array format it looks more difficult for me.

 "outputs": {
    "keyVaultName": {
        "type": "string",
        "value": "[variables('keyVaultName')]"
    },
    "websitesname": {
        "type": "array",
        "copy": {
            "count": "[length(variables('webSitesName'))]",
            "input": {
                "name": "[variables('webSitesName')[copyIndex()].name]"
            }
        }
    }
}

Then i do this:

$deploymentResult = New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName 
$ResourceGroupName-TemplateFile $TemplateFile 
$vaultName = $deploymentResult.Outputs.keyVaultName.Value
$arrayWebsitesName = $deploymentResult.Outputs.websitesname.Value

enter image description here

I need to extract value from returned array. In powershell i expected to use something like

  • $arrayWebsitesName[0] to get AppService-Prod-CentralUS
  • $arrayWebsitesName[\1] to get AppService-Prod-EastUS

I believe there is something related to conversion and I tried convertto-json without success. Your help is appreciated !


Solution

  • The variable type returned from the deployment for websitesname is a JArray.

    PS> $deploymentResult.Outputs.websitesname.Value.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    JArray                                   Newtonsoft.Json.Linq.JContainer
    

    In order to get at the values, you would make the call like:

    $arrayWebsitesName = $deploymentResult.Outputs.websitesname.Value
    $ids = $arrayWebsitesName.ToString() | ConvertFrom-Json
    $ids[0].name
    $ids[1].name
    

    Converting the JArray to a JSON string, and then converting the JSON string to a PowerShell array may not be most efficient but I don't typically deal with large arrays and performance is not a concern (gets the job done).