Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This module requires a sandbox to complete. A sandbox gives you access to free resources. Your personal subscription will not be charged. The sandbox may only be used to complete training on Microsoft Learn. Use for any other reason is prohibited, and may result in permanent loss of access to the sandbox.
Microsoft provides this lab experience and related content for educational purposes. All presented information is owned by Microsoft and intended solely for learning about the covered products and services in this Microsoft Learn module.
Now that we've created a virtual machine, we can get information about it through other commands.
Let's start by running vm list
.
az vm list
This command will return all virtual machines defined in this subscription. You can filter the output to a specific resource group through the --resource-group
parameter.
Notice that the default response type for all the commands we've done so far is JSON. This is great for scripting, but most people find it harder to read. You can change the output style for any response through the --output
flag. For example, run the following command in Azure Cloud Shell to see the different output style.
az vm list --output table
Along with table
, you can specify json
(the default), jsonc
(colorized JSON), or tsv
(Tab-Separated Values). Try a few variations with the preceding command to see the difference.
Another useful command is vm list-ip-addresses
, which lists the public and private IP addresses for a VM. If they change, or you didn't capture them during creation, you can retrieve them at any time.
az vm list-ip-addresses -n SampleVM -o table
This returns output like:
VirtualMachine PublicIPAddresses PrivateIPAddresses
---------------- ------------------- --------------------
SampleVM 168.61.54.62 10.0.0.4
Tip
Notice that we're using a shorthand syntax for the --output
flag as -o
. You can shorten most parameters to Azure CLI commands to a single dash and letter. For example, you can shorten --name
to -n
and --resource-group
to -g
. This is handy for entering keyboard characters, but we recommend using the full option name in scripts for clarity. Check the documentation for details about each command.
We can get more detailed information about a specific virtual machine by name or ID running the vm show
command.
az vm show --resource-group "[sandbox resource group name]" --name SampleVM
This returns a fairly large JSON block with all sorts of information about the VM, including attached storage devices, network interfaces, and all of the object IDs for resources that the VM is connected to. Again, we could change to a table format, but that omits almost all of the interesting data. Instead, we can turn to a built-in query language for JSON called JMESPath.
JMESPath is an industry-standard query language built around JSON objects. The simplest query is to specify an identifier that selects a key in the JSON object.
For example, given the object:
{
"people": [
{
"name": "Fred",
"age": 28
},
{
"name": "Barney",
"age": 25
},
{
"name": "Wilma",
"age": 27
}
]
}
We can use the query people
to return the array of values for the people
array. If we just want one of the people, we can use an indexer. For example, people[1]
would return:
{
"name": "Barney",
"age": 25
}
We can also add specific qualifiers that would return a subset of the objects based on some criteria. For example, adding the qualifier people[?age > '25']
would return:
[
{
"name": "Fred",
"age": 28
},
{
"name": "Wilma",
"age": 27
}
]
Finally, we can constrain the results by adding a select: people[?age > '25'].[name]
that returns just the names:
[
[
"Fred"
],
[
"Wilma"
]
]
JMESQuery has several other interesting query features. When you have time, check out the online tutorial available on the JMESPath.org site.
With a basic understanding of JMES queries, we can add filters to the data returned by queries like the vm show
command. For example, we can retrieve the admin username:
az vm show \
--resource-group "[sandbox resource group name]" \
--name SampleVM \
--query "osProfile.adminUsername"
We can get the size assigned to our VM:
az vm show \
--resource-group "[sandbox resource group name]" \
--name SampleVM \
--query hardwareProfile.vmSize
Or, to retrieve all the IDs for your network interfaces, we can run the query:
az vm show \
--resource-group "[sandbox resource group name]" \
--name SampleVM \
--query "networkProfile.networkInterfaces[].id"
This query technique works with any Azure CLI command, and you can use it to pull specific bits of data out on the command line. It's useful for scripting, as well. For example, you can pull a value out of your Azure account and store it in an environment or script variable. If you decide to use it this way, it's useful to add the --output tsv
parameter (which you can shorten to -o tsv
). This will return results that only include the actual data values with tab separators.
For example:
az vm show \
--resource-group "[sandbox resource group name]" \
--name SampleVM \
--query "networkProfile.networkInterfaces[].id" -o tsv
returns the text: /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/providers/Microsoft.Network/networkInterfaces/SampleVMVMNic
Having an issue? We can help!
Please sign in to use this experience.
Sign in