Azure CLI Interactive Mode
The Azure CLI has a feature that I’m not sure many people are aware of. Certainly, I haven’t seen it being used much. This feature is called “interactive mode”.
You can enter interactive mode in the CLI by running the command az interactive
. Once you do, you’ll be presented with a layout that looks like this:
Aside from a more colourful layout, this interactive prompt has several features that can be useful for anyone working with the CLI, and especially to those that are new to it, or if you are working with commands that are new to you.
Autocomplete
The first feature you’ll notice is that you know have auto-completion available. As you type commands, the console offers you lists of available commands to come next. In the image below, I’m being offered all the commands that can be used with “az vm”.
This auto-completion also extends to parameters, where not only will it show the available parameters, but also some help text describing each parameter.
Not only can it show parameters, but it is also able to suggest values for these parameters from your subscription. For example, when running the “az vm show” command to get details of a VM, the console can offer values for the “name” parameter, listing VMs in my currently active subscription.
Help Text
As I type commands and use the autocomplete, the CLI will present me with help text explaining the specific command I am currently on. It also shows some examples of how to run that command and what parameters to use to achieve specific goals.
Where there are multiple examples, you can view a specific one in more detail by entering the number of the example:
vm create ::3"
Command Scope
By default, all commands you run in interactive mode are scope to the az group, so for example, if I want to list VMs I would type “vm list”, the “az” part is completed for me. I can, however, change the scope to be whatever I need. For example, if I am doing a lot of work with availability sets and typing “az vm availability-set” is becoming tiresome, I can enter the command “%%vm availability-set”, and my console will be grouped to that:
Now all my commands are scoped to an availability set, so to list all availability sets all I need to enter is the command “list”
To revert to the default scope (az), I can enter %%.
JMESPath Queries
The Azure CLI has built-in support for querying the JSON results using JMESPath. The interactive prompt enhances this with the “??” command, which allows you to reference the previously run command as part of a query. For example, if I have queried a VM with this command:
vm show --name aks-nodepool1-30108646-0 -g MC_samcogancom_AKS_samcogancomAKS-EU_westeurope
This command returns the VM object as a long JSON string. To query that result to get the ID of the VM, I can now run:
"?? id"
so it will fetch the ID from the result of the previous command.
We can query further down the tree using dot notation:
"?? hardwareProfile.vmSize"
We can also use the result of a previous query in a new command; for example, the command below will get all availability sets in the same resource group as the VM in the last query.
vm availability-set list --resource-group "?? resourceGroup"
External Commands
The interactive shell is primarily concerned with running az commands, however, if you do need to run other commands you can do so using the “#” command. This allows you to call out to native commands you are running in, such as Bash on Linux and CMD on Windows.