- 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
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.
The path you specify is similar to the following:
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:
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.
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 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:\>
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:
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 126.96.36.199 southcentralus winclient1 TESTING VM running 188.8.131.52 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 184.108.40.206 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.