Use az cli to Query Multiple Fields of Resource Information

Use az cli to query multiple fields of VM information. Here we need to use JMESPath language to implement it.

Typically, we will use az vm show to get the detailed VM information:

$ az vm show -g Linux -n alpha -d -o table
Name    ResourceGroup    PowerState    PublicIps     Fqdns    Location    Zones
------  ---------------  ------------  ------------  -------  ----------  -------
alpha   Linux            VM running    11.1.111.111           eastasia    1

However, this table doesn't contains the VM size. If we remove the -o table argument, the VM size will be shown like this:

$ az vm show -g Linux -n alpha -d
{
  "additionalCapabilities": {
    "hibernationEnabled": false,
    "ultraSsdEnabled": null
  },
...
  "hardwareProfile": {
    "vmSize": "Standard_D2lds_v5",
    "vmSizeProperties": null
  },
...
}

Then we can get VM information with queries:

$ az vm show -g Linux -n alpha -d --query 'hardwareProfile.vmSize'
"Standard_D2lds_v5"

Can we get the powerState and the vmSize at the same time? az vm show -g Linux -n alpha -d --query 'hardwareProfile.vmSize' --query 'powerState' doesn't work. So how to query multiple fields of the json?

The answer is using JMESPath language, putting all the required fields in the []:

$ az vm show -g Linux -n alpha -d --query '[name, resourceGroup, powerState, hardwareProfile.vmSize]' -o tsv
alpha
Linux
VM running
Standard_D2lds_v5

References:

# Azure CLI - Query Power state of a virtual machine