The Microsoft Azure command-line interface (CLI) v2.0 is fast and cross platform, but it's not particularly intuitive. That's why the Azure Tools group developed the Azure CLI interactive shell. Let's learn how to use it.

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:

az-shell

Exit the shell this way or by pressing Ctrl+D:

exit

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.

Azure CLI v2.0 shell interface

Azure CLI v2.0 shell interface

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:

login

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:

Azure CLI autocompletion

Azure CLI autocompletion

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.

Live autocomplete of your Azure resource data

Live autocomplete of your Azure resource data

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.

Tabular output instead of JSON

Tabular output instead of JSON

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:

az shell provides live documentation as you type

az shell provides live documentation as you type

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:

#cls

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

Wrap-up ^

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.

0 Comments

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account