- Interact with Azure Cosmos DB with PowerShell - Tue, Sep 14 2021
- Azure health services: Track Microsoft cloud outages and maintenance - Wed, Sep 8 2021
- Powerline: Customize your PowerShell console - Tue, Aug 31 2021
In September 2016, Microsoft announced the public preview of its Azure Command-Line Interface (CLI) v2.0. This is a complete rewrite (in Python) of their original cross-platform CLI (xplat-cli) developed in Node.js. Microsoft maintains Azure CLI v1.0 for compatibility with the legacy Azure Service Management (ASM) deployment model.
The idea behind the Azure CLI is that Azure administrators and developers should have a toolset that:
- Runs on Windows, macOS, or Linux
- Is fast and intuitive
- Is open source and welcomes community contributions
- Grows and adapts to the Azure platform's daily changes
If Azure CLI v2.0 has a weakness, it's the tool's non-interactive nature. The toolset behaves much like the ancient netsh.exe executable in which you execute stateless commands one at a time.
Today I'll introduce you to the nifty interactive shell Microsoft built on top of Azure CLI v2.0. I like it a lot, and I believe you will as well.
Installing the Azure CLI v2.0 shell ^
Before you install az-shell, you need to install and configure Azure CLI v2.0. If you haven't already done so, read my tutorial and follow the directions to take care of this prerequisite I described in my article about Azure CLI v2.0.
Now you can install the Azure CLI v2.0 shell with a single pip command invocation:
pip install --user azure-cli-shell
Start the interactive shell this way:
Exit the shell this way or by pressing Ctrl+D:
Easy, right? Now let's dive deeper.
Command discovery ^
The following screenshot shows the az-shell interface after I've pressed F3 to display default key bindings.
The first thing you'll notice is that this is an interactive environment. In other words, you don't need to prefix commands with az like you do in the native Azure CLI v2.0 shell.
Second, you have a nano-esque footer with some keyboard shortcuts. They are:
- F1: Specify whether you want the shell to display command descriptions, parameter descriptions, or examples
- F2: Lists user-configured default settings
- F3: Toggles the keyboard shortcut list
- CTRL+D: Exits the interactive shell
- Subscription: Shows the active Azure subscription
Hey, we need to log into Azure before we go any further:
It will direct you to https://aka.ms/devicelogin and prompt you to enter the one-time token the CLI provides.
If you use an Azure AD security principal instead of a Microsoft Account (MSA), you can log in without leaving the Azure CLI window.
The third thing you'll notice is IntelliSense autocompletion for all commands and even parameters. For example, let's say we want to list all virtual machines (VMs) defined in our subscription. Before we get that far, it would be nice to get some syntax help, right?
All we have to do is start typing. Look here:
As you can see in the previous screenshot, as soon as you type vm <space>, az-shell drops down a menu of command subgroups. Press Tab to complete the currently selected command until your statement is complete.
In this case, after vm list you can add --resource-group, after which az-shell actually feeds you live data from your subscription! This is an awesome feature; I show you what this looks like in the next screenshot.
By the way, up arrow and down arrow cycle you through your session command history just like you're accustomed to.
Azure CLI v2.0 presents output in JSON, which may or may not be what you need. Add the -o parameter to the command string to override. Personally, I prefer tabular output.
In the previous screenshot, do you notice how I specified -g instead of --resource-group and -o instead of –-output? How did I know those shortcuts were available?
Well, if you opt in to inline documentation (by pressing F1 and answering all questions with 'yes'), you'll see command descriptions, syntax help, and examples in the lower window pane as shown here:
Some of the on-screen help goes past one screen. For instance, look at the following:
How can we see past the first help screen?
You can use F3 to expose the relevant "cheat keys," but let me tell you that:
- Ctrl+N: Scrolls forward through multi-screen help
- Ctrl+Y: Scrolls backward through multi-screen help
Another "cheat key" I like a lot is the octothorpe (#), also called the pound sign. This is an escape character that lets us run external commands. For instance, type the following to clear the screen:
Deploy a new virtual machine the CLI way ^
To finish up, let's quick-create an Ubuntu Linux virtual machine in a new Azure resource group called test1. First, we'll create the resource group:
group create -l westus -n test1
Second, we'll deploy the VM. Admittedly, we're using defaults a lot, but this is just a proof of concept!
vm create -g test1 -n linuxvm1 --image ubuntults
We can destroy that VM immediately by issuing a single command:
vm delete -g test1 -n linuxvm1 --yes
What do you think? Azure CLI v2.0 presents a nice cross-platform alternative to Azure PowerShell. In fact, I have some Azure administrator friends who prefer Azure CLI v2.0 due to its great speed.
Once you've gotten past your initial syntax and usage learning curve, the Azure CLI v2.0 is an agile tool. I give the Azure Tools development team great credit in extending the CLI with this interactive shell.
Subscribe to 4sysops newsletter!
To be sure, I'm nearly 100 percent positive that I'll never again use the Azure CLI without the interactive shell.