denisismagilov - Fotolia

Tip

Evaluate Azure CLI vs. PowerShell for resource management

Compare two popular resource management tools for Azure -- Azure CLI and PowerShell -- to determine if one, or a combination of both, is right for your cloud environment.

When it comes to resource management in Microsoft Azure, there are two primary tools IT teams can use: Azure CLI and PowerShell. One question that comes up frequently is when to use one or the other.

Sometimes the choice between the two tools depends on what is familiar. For those coming from an on-premises environment, PowerShell is the more natural choice since it's been a part of Microsoft Windows for more than a decade, but the Azure command-line interface (Azure CLI) has its merits, too.

The differences between the two are often subtle, and your selection will likely come down to personal preference. Thankfully, though, the process of choosing is made easier by the fact that the Azure CLI and PowerShell call the same exact APIs. Let's take a closer look at Azure CLI and PowerShell, as well as the possibility of combining them.

What are Azure CLI and PowerShell?

Before jumping into the code, let's talk briefly about PowerShell and the Azure CLI and why they are so popular.

Azure CLI

The Azure CLI is a set of commands developers use to create, manage and interact with all Azure resources from a programmatic perspective. The Azure CLI is built in Python. You can use the Azure CLI from both the terminal on your computer and the Azure Cloud Shell, which is a browser-accessible shell.

Additionally, Azure CLI is cross-platform, which means IT teams can use it on any OS, including:

  • Linux distributions
  • MacOS
  • Windows

PowerShell

While it was originally only for Windows, PowerShell was re-created as an open source scripting and programming language with an interactive command-line shell. Microsoft built it to serve as an automation tool for system administrators. Unlike most command-line shells, PowerShell was built on the .NET framework and works with objects.

Similar to the Azure CLI, IT teams can run PowerShell on any OS. Developers can also use it to interact with any platform or application that has a public-facing API. PowerShell is still one of the top languages used, alongside Python and JavaScript, for scripting and automating in Azure.

3 ways to create an Azure resource group

From a comparison perspective, the best way to see PowerShell and Azure CLI in action is to create a resource and a PowerShell function. By creating a resource, you can see the differences between code length, ease of use and speed.

First, Let's create an Azure resource group using a cmdlet, which is a scaled-back version of a PowerShell function:

New-AzResourceGroup -Location eastus2 -Name resource_group_name

Now, let's look at the same task using Azure CLI:

az group create -l eastus2 -n resource_group_name

As you can see, the length is pretty much the same. However, one thing that you can do with PowerShell natively is expand the cmdlet and create a PowerShell Tool out of it. This is sometimes referred to as Toolmaking.

Toolmaking is great if you want to add functionality to the PowerShell cmdlet or restrict what's available. For example, you could use this technique to restrict which parameters are exposed:

function New-ResourceGroup {
	param (
	    [parameter(Mandatory)]
	    [string]$rgName,

	    [parameter(Mandatory)]
	    [string]$location
	)

	$params = @{
	    'Name' = $rgName
	    'Location' = $location
	}	

	New-AzResourceGroup @params
}

Now, you're more likely to use the New-AzResourceGroup command, but the point of this example is to show that you can extend the cmdlet with additional functionality that you don't have with a command-line tool like Azure CLI. That's because PowerShell gives you more "programmer" type options, including parameters, error testing and more.  

Combine PowerShell and Azure CLI

One benefit of these two tools is developers can combine them for more functionality. Azure CLI is a good choice, but there's no solid way to make the code reusable by itself. For example, if someone else wants to use the code and change the name of the resource group that's being created, developers have to create variables in a terminal session and then the code can run. When you add PowerShell into the mix, you get additional features such as error handling and reusability.

As you can see in the example below, the code uses a PowerShell function and adds in the Azure CLI. This ensures that the Azure CLI has reusable code and error handling added in, to make it a true script.

function New-ResourceGroup {

     param (
          [parameter(Mandatory)]
          [string]$rgName,
          [parameter(Mandatory)]
          [string]$location
     )

     try {
          az group create -l $location `
                          -n $rgName
     }

     catch {
          $pscmdlet.ThrowTerminatingError($_)
     }
}

Combining both PowerShell and Azure CLI is a great approach if you cannot decide which one to use.

Use Azure CLI with other programming languages

Let's say you're a Python developer and you want to try PowerShell, but the rest of the stack you work on is Python, so you decide to stay with Python. However, IT teams in this scenario can still use the Azure CLI with other programming languages.

Below is an example of using the Azure CLI with Python. If you're a Python developer, as you can see, you're writing Python code the same way you would always write it. There's no difference in syntax.

The azure.cli.core library enables you to use an invoke function, which you can use to type out an Azure CLI command. This scenario is listing VMs in a specific resource group.

from azure.cli.core import get_default_cli as azcli
import logging
import sys


def list_vms(resource_group):
     azcli().invoke(['vm', 'list', '-g', resource_group])

resource_group = sys.argv[1]

if __name__ == '__main__':
     print('Running as main program...')
     list_vms(resource_group)

Editor's note: This story was updated after publication.

Dig Deeper on Cloud infrastructure design and management

Data Center
ITOperations
SearchAWS
SearchVMware
Close