- 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
I’m almost positive you know how robust the Windows PowerShell support is in Microsoft Azure. With the release of Azure PowerShell v1.0, you can accomplish just about every administrative task in either the classic service management or new-ish Resource Manager deployment models.
As awesome as Windows PowerShell is, we’re left with a question: how can we access our Azure resources via a command prompt when we’re working in Linux or OS X? Yeah, we can bring up a Windows virtual machine or start an RDP session, but that defeats the purpose of administrative automation. Well, enter the Azure Command-Line Interface (CLI).
To get started, visit the Azure Software Development Kits (SDKs) and Tools page and download the Azure CLI installer (scroll down to Command-line tools / Azure command-line interface) that’s appropriate for your OS. The Azure Documentation has clear instructions for performing Linux-based installations and invoking the Azure CLI from a Docker container.
I want to prove to you that the Azure CLI is truly cross-platform, so I’ll be working from my Apple iMac running OS X El Capitan. Microsoft did a good job with their OS X installer!
Installing the Azure CLI on my Mac
Logging in and logging out ^
At your command line (Terminal in OS X or Linux; elevated PowerShell console on Windows), simply type azure to view the “welcome” page. By the way, the output is exactly the same, regardless of your OS.
The Azure CLI welcome text
Logging in to your Azure subscription involves four steps:
- Running the command azure login
- Navigating to the sign-in web page
- Submitting your access token
- Returning to the Azure CLI session
For instance, this is the output I received when I ran azure login on the Mac:
azure login info: Executing command login |info: To sign in, use a web browser to open the page https://aka.ms/devicelogin. Enter the code ARV2EZVBN to authenticate.
I needed to copy the URL link (OS X actually allows you to click hyperlinks directly from the Terminal window) to a browser and submit the access token. When I did this on Windows, I was also required to submit my Azure username and password.
Azure CLI’s browser-based authentication method
You can use the publish settings file approach if you need to automate connecting to your Azure subscription. Also, my scope in today’s lesson will be strictly in service management rather than resource management.
After login, you’ll receive information about your Azure subscription(s), as well as an indication of which is your default. I have multiple Azure subscriptions, so this is what I see:
azure login info: Executing command login \info: To sign in, use a web browser to open the page https://aka.ms/devicelogin. Enter the code ARV2EZVBN to authenticate. /info: Added subscription 50dollar1 info: Added subscription 150dollar info: Added subscription 50dollar2 info: Setting subscription "50dollar1" as default + info: login command OK
Note that the Azure CLI isn’t an interactive environment. In other words, you need to prefix all your Azure CLI command calls with azure. We use azure account list to view our subscriptions, and azure account set to specify a default account. I’ll use my 150dollar account as my default subscription today:
azure account set 150dollar info: Executing command account set info: Setting subscription to "150dollar" with id "6be05db5-1fba-4dde-b309-ebbc94f0684e". info: Changes saved info: account set command OK
Now about getting help. Run azure help with no arguments to see a command list. It’s so brief that I’ll reprint the output in full here:
Commands: account Commands to manage your account information and publish settings config Commands to manage your local settings hdinsight Commands to manage HDInsight clusters and jobs mobile Commands to manage your Mobile Services network Commands to manage your networks sb Commands to manage your Service Bus configuration service Commands to manage your Cloud Services site Commands to manage your Web Sites sql Commands to manage your SQL Server accounts storage Commands to manage your Storage objects vm Commands to manage your Virtual Machines
So, if we need to see command help for website-related tasks, we’d type:
azure help site
Don’t type help site if you’re in a PowerShell session or you’ll run Get-Help to find commands that include “site” in their names! Remember—all Azure CLI calls need to start with “azure” and a space.
We can continue drilling down with the azure help command. For instance, to learn how to add a new database connection string programmatically to one of your sites, run this:
azure help connectionstring add
And so on. Pretty straightforward. When you’re ready to end your Azure CLI session, don’t forget to gracefully connect by running azure logout.
azure logout email@example.com info: Executing command logout info: You have logged out. info: logout command OK
Managing Azure resources with the CLI ^
Azure CLI defaults to using Azure service management, but you can be sure of this by running azure config mode asm. By contrast, you run azure config mode arm if you want to use Resource Manager.
Today I’d like to focus on Azure Web Apps (formerly known as “Azure Websites”). We’ll start by listing my sites in the current subscription. And, yes, I’m aware that the following screenshot was taken from a Windows box. 😉
Listing Azure web apps from the Azure CLI
By the way, I created the above DEV and PROD deployment slots like this:
azure site create 4sysops --slot dev azure site create 4sysops --slot prod
We can easily swap our web app between deployment slots like so. The default slot is always named “production,” and here I’m swapping the 4sysops web app into the DEV slot. Under the covers, Azure actually duplicates the web app so they exist in both slots under different URLs:
azure site swap 4sysops 'production' 'dev' info: Executing command site swap + Getting sites Swap slot "production" from site "4sysops" with slot "dev" ? [y/n] y + Swapping slot "production" from site "4sysops" with slot "dev" info: Site "4sysops" slots has been swapped info: site swap command OK
Next steps ^
What do you think of the Azure CLI? Do you envision yourself using it from your Mac or Linux computer? I’ll leave you with some additional help resources to get you past your Azure CLI learning curve as quickly as possible:
- Xplat-CLI cheat sheet
- Use the Azure CLI with ASM
- Use the Azure CLI with ARM
- Difference Between Azure ASM and ARM
- Azure Websites Cheat Sheet