The Microsoft Azure development teams recently released version 2.0 of their cross-platform command-line interface (CLI). Learn what the tool is, how to use it, and how it compares to Azure PowerShell.

Amazon Web Services (AWS) has had a cross-platform command-line interface (CLI) for a few years now. In mid-2016, Microsoft gave us Azure professionals analogous functionality in the Azure CLI, originally called the cross-platform (XPlat) CLI. Both projects are open-sourced at GitHub:

Azure CLI v1.0 is a Node.js application and supports both the Classic Azure Service Management (ASM) and Azure Resource Manager (ARM) application programming interfaces (APIs).

Azure CLI vs. Azure PowerShell ^

In 2017, we have Azure CLI v2.0, a re-architected product now based on Python. Before you begin learning about the Azure CLI, you may wonder why you should care. After all, we have Azure PowerShell, right?

In my experience, here are the main things that Azure CLI brings to the Azure professional:

  • consistent experience across Windows, macOS, and Linux
  • easy integration with shell scripts
  • simpler syntax than Azure PowerShell or the bash shell

Install Azure CLI v2.0 on Windows 10 ^

I mentioned that Azure CLI v2.0 is a Python application. Therefore, you'll need to install the Python interpreter and tools on your Windows management workstation. Visit python.org/downloads and snag a v3.x version as the documentation suggests.

For convenience, I'd suggest that you add the Python executable to your computer's search path. As you can see in the next screenshot, the default installation includes the pip package manager, which will come in handy in the next step.

Install Python using (mostly) the default options

After installing Python, fire up an elevated PowerShell console and use pip to install the latest Azure CLI v2.0 release:

pip install --user azure-cli

NOTE: What you've done here is download and install the latest release version (as Microsoft published) in the Python Package Index into the currently logged-on user's context. For more information on pip, read the docs.

We're not done yet though. The Python installer will add the interpreter to your system path, but you also need to open the system Control Panel and add the Azure CLI Python script path to your system's PATH environment variable as shown in the following figure.

Add the Azure CLI script directory to your PATH

Add the Azure CLI script directory to your PATH

The path you specify is similar to the following:

C:\Users\username\AppData\Roaming\Python\pythonversion\Scripts\

In this path, username is your user name and pythonversion corresponds to your chosen Python version.

Log into Azure and set the proper subscription ^

We're now ready to begin using the Azure CLI v2.0! Restart an elevated PowerShell console and run the following command to authenticate to Azure:

az login

To sign in, use a web browser to open the page https://aka.ms/devicelogin and enter the code ENE6PUA2J to authenticate.

The az login command generates a one-time token that you paste into the provided URL. It'll then prompt you to enter your Azure credentials as usual. I show you the authentication token web page in the next screenshot.

Authenticate to Azure from the CLI

Authenticate to Azure from the CLI

It's crucial that you understand that Azure CLI v2.0 is not an interactive shell like PowerShell. Instead, the az command works like the old netsh command-line utility. For example, try running az with no additional arguments; you'll see output similar to this:

The az command default output

The az command default output

The default az command output lists the top-level contexts with which we can use the environment. For instance, the az account context deals with your Azure subscriptions. The best advice I have for you is to get into the habit of appending -h to your az commands, so you can see what the next part of the command is. Check this out:

PS C:\> az account -h
Group
    az account: Manage subscriptions.
Commands:
    clear         : Clear all subscriptions from the CLI's local cache.
    list          : Get a list of subscriptions for the account.
    list-locations: List supported regions of the current subscription.
    set           : Set a subscription as the current subscription.
    show          : Show the details of a subscription.
PS C:\>

Thus, you can run az account list to get a record of your Azure subscriptions. Remember that the Azure CLI, like PowerShell, interacts with Azure by means of calling representational state transfer (REST) APIs that use a JavaScript object notation (JSON) payload. To that point, you'll receive JSON as your output unless you override with the ‑‑output flag:

PS C:\> az account list --output table
Name          CloudName    SubscriptionId    State    IsDefault
------------- -----------  ----------------- -------  -----------
tim-2017    AzureCloud   0c1c1adb-b39c-4c75  Enabled  True
50-Jan-31   AzureCloud   3d935138-40b5-408c  Enabled
150-Jan-19  AzureCloud   7be05db5-0dea-4ffe  Enabled
PS C:\>

NOTE: If you used the first Azure CLI version, you're accustomed to calling the azure command. Forget about that in Azure CLI v2; the command is az.

As you can see, I have a few Azure subscriptions. What if I need to set the 150-Jan-19 subscription as my default? Running az account -h reveals a set verb; eventually I get to this command string that works:

az account set --subscription 150-Jan-19

Let me summarize your suggested workflow for getting accustomed to the Azure CLI:

  • You need to run az every time you call a Azure CLI command; there is no interactive environment.
  • The default output is JSON, but you can override it with ‑‑output, specifying either table, tsv, json, or jsonc.
  • Habitually append -h to your commands to help you "build" the entire command and learn the syntax.

View some Azure resources ^

Now that we are authenticated to Azure and are focused on the correct subscription, let's view some Azure resources. How about virtual machines (VMs)?

Running az tells us there is a vm context. We can guess there is a -list verb, so let's get some context-sensitive help. I show you partial output in the next screenshot:

Getting context sensitive help

Getting context sensitive help

Look at the output in the previous screenshot. What I want to draw your attention to is that Azure CLI v2.0 parameters have long and short versions. Which one you use is up to you, but I wanted to call that out anyway.

This means we can run:

PS C:\> az vm list --output table
Name        ResourceGroup    Location
----------  ---------------  --------------
sqlserver1  TESTING          southcentralus
winclient1  TESTING          southcentralus
PS C:\>

Or we could run az vm list -o table and get the same results. Some of my colleagues love the potential for great speed with the CLI. The less typing, the better, right?

Check the -h help to figure out what I did here:

PS C:\> az vm list -g TESTING -d -o table
Name        ResourceGroup    PowerState    PublicIps      Location
----------  ---------------  ------------  -------------  --------------
sqlserver1  TESTING          VM running    13.65.208.137  southcentralus
winclient1  TESTING          VM running    40.74.252.120  southcentralus
PS C:\>

Create a new resource with the CLI ^

Let's wrap up this brief tutorial with a "lightning round" VM creation demonstration. For simplicity, let's say we want to deploy a Windows Server 2012 R2 Datacenter VM into an existing resource group:

az vm create --resource-group testing --name win2012vm --image Win2012R2Datacenter

After completing the deployment, you can use the az vm show command to see some details:

PS C:\> az vm show -n win2012vm -g testing -d -o table
Name       ResourceGroup    PowerState    PublicIps     Location
---------  ---------------  ------------  ------------  --------------
win2012vm  testing          VM running    13.85.85.127  southcentralus
PS C:\>

The CLI will prompt you interactively for any required parameters that you didn't specify on the command line. This is an admittedly anemic example; check the docs for a step-by-step tutorial on deploying a Linux VM using the CLI.

Closing thoughts ^

The Azure CLI v2.0 reached generally available (GA) status only in late February 2017. Therefore, you'll find that the documentation is barebones (to put it charitably) and that the tool likely lacks features you may require. Be patient! We community members can participate actively in the CLI's development, as it is an open-source project. You can submit issues or even fork the project to your GitHub account and submit bugfixes and new features by using the pull request system.

Subscribe to 4sysops newsletter!

The long story short is that the Azure CLI takes a cue from the Amazon AWS CLI and provides a fast scripting and automation environment for Azure that works on Windows, macOS, and Linux.

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