Powershell Version: 5.1.22621.1778 Azure CLI Version: 2.53.0
I am using the Azure CLI to execute commands in a cloud subscription. My issue is that I want to assign the JSON result to a PowerShell variable.
When I do this for an array result, the PowerShell variable contains a string array, line by line, of the cli result, e.g.
$result = az group list -o json"
Returns:
[ { "id": "/subscriptions/xxx/resourceGroups/group1", "location": "xxx", "managedBy": null, "name": "group1", "properties": { "provisioningState": "Succeeded" }, "tags": {}, "type": "Microsoft.Resources/resourceGroups" } ]
I would expect that $result contain an array of resource groups, if 1 group is returned like the example above, then:
$result.count should equal 1
However, in this case $result.count == 13 Furthermore, $result.GetType() == System.Array
It appears that $result is an array of the string lines returned, rather than a JSON array of the data.
I have a work around, that seems like a bit of hack:
$result = (az group list -o json | out-string) $groupList = ConvertTo-Json ($result)
Basically, convert the result to a string and then convert it back into JSON.
Anyone know a better way or why this is happening?
You want to use ConvertFrom-Json:
$groupList=$(az group list -o json | ConvertFrom-Json)