Install the Azure CLI v2.0 on Windows

PROBLEM: You need to author an Azure automation script for your colleague before you go on vacation, but he operates Mac and Linux computers exclusively.

SOLUTION: Install the Azure CLI on your Windows PC and share a cross-platform script.

The beauty of the Azure Command-Line Interface (CLI) is that it’s an open-source, cross-platform scripting environment for Windows, macOS, and Linux.

As is the case with Azure PowerShell, Azure CLI commands abstract the underlying Azure Resource Manager (ARM) APIs. The original Azure Cross-Platform CLI (Xplat-CLI) is a Node.js application that the Azure product groups continue to maintain for backward compatibility with Azure Service Management (ASM).

Today I’ll teach you how to install the Python-based Azure CLI v2.0, which is in public preview as of this recording, on a Windows administrative workstation.

The Azure CLI v2.0 works only with Azure Resource Manager (ARM).

Step #1: Install Python

Because Azure CLI v2.0 is a Python application, you’ll need to install the Python interpreter. Download the latest 3.x release from the Python website and run the wizard-based installer; I suggest you enable the Add Python 3.5 to PATH option as shown in Figure 1 so you can call python from wherever you are in the command line.

python-installer
Figure 1. Python installer.

Verify that you’ve successfully installed Python by firing up an elevated PowerShell console and running the following command:


python --version


NOTE: I suggest you install the Azure CLI v2.0 only on Windows client systems, and use Python 3.5.x as noted in the ever-evolving documentation.


Step #2: Install the CLI Using PIP

The Azure CLI v2.0 development team hosts the project on GitHub, but your best bet for installing the latest stable release quickly is to employ the Python Package Index (PIP). From your PowerShell session run the following command:


pip install azure-cli

You’re now ready to use the Azure CLI to perform ad-hoc Azure administration or to include Azure CLI statements in your shell scripts.

Get to Work

If you’ve used Azure CLI before, then you know that it isn’t an interactive environment like PowerShell is. Instead, you prepend the az keyword to every statement you run. For example, you’d run the following command from your PowerShell prompt to list all VMs contained in a particular resource group:


az vm list --resource-group AzureTraining


NOTE: Azure CLI v1 uses the azure keyword, and Azure CLI v2.0 uses the az keyword. Scripts are not compatible between Azure CLI versions.


You authenticate to Azure CLI by running az login; doing so prompts you to open the URL https://aka.ms/devicelogin in a browser and paste the access token. I show you this in Figure 2.

azure-cli-login
Figure 2. Azure CLI login process.

After you enter your access token, supply your Azure credentials as usual. I’ll  provide you with more Azure CLI v2.0 instruction as the blog evolves, but for now let me give you the quickest jump-start possible.

Append -h at any point to see context help. Let’s say you have multiple Azure subscriptions and you need to ensure the CLI is attached to the correct one. Try this:


az account -h

Group
az account: Commands to manage subscriptions.

Commands:
clear : Clears all subscription 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 : Makes a subscription the current subscription.
show : Show the details of a subscription.

The CLI returns JSON by default. You can change that by using the –output flag like so:


az account list --output list

As usual, consult the documentation for more robust Azure CLI v2.0 syntax help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s