Azure AZ CLI

Azure AZ CLI


Intro

There are two versions of Azure CLIs: the older Azure PowerShell AzureRM shell and the newer Azure PowerShell Az 

 


Documentation



 


Tips and Tidbits

  • You can install it locally on computers running the Linux, macOS, or Windows operating systems.

    • You can also use Azure CLI from a browser through Azure Cloud Shell.

  • Azure CLI can be used interactively or through scripts

  • how do you find the particular commands you need? One way is to use az find.

  • If you already know the name of the command you want, the --help argument for that command will get you more detailed information on the command

  • Az is the formal name for the Azure PowerShell module, which contains cmdlets to work with Azure features.

  • This module is an open-source component available on GitHub.

 


Installing Azure AZ CLI



 

Uninstall-AzureRm
  • Confirm it has been uninstalled

Get-InstalledModule -Name AzureRM -AllVersions Get-Module -Name AzureRM -ListAvailable
  • Install the Azure Az PowerShell modules. Accept the warning about untrusted repo.

Install-Module -Name Az -AllowClobber -Scope CurrentUser
  • Confirm module is installed

Get-InstalledModule -Name Az -AllVersions

 

 

 


Using A Specific Version

If you need to install a specific version

# Install Az version 3.6.1 Install-Module -Name Az -RequiredVersion 3.6.1 -AllowClobber -Scope CurrentUser

 

You can load a specific version of the Az module using the -RequiredVersion parameter:

# Load Az version 3.6.1 Import-Module -Name Az -RequiredVersion 3.6.1

 


Sign-In To Azure

 

Check any default account settings

az account show

 

PS C:\Users\Roger> az account list -o table Name CloudName SubscriptionId State IsDefault ------------------------ ----------- ------------------------------------ ------- ----------- Concierge Subscription AzureCloud 660e4eb5-0b62-4a64-b29a-ec39271b86df Enabled False Concierge Subscription AzureCloud 4db700a1-ce71-4523-b484-93f5d1306b32 Enabled False Azure Pass - Sponsorship AzureCloud 5b9ca790-db4a-4052-a139-7a03d8c47bbd Enabled True

If incorrect, or if you have multiple subscriptions or tentants, you can specify them in the command line.

az account set --subscription "<subscription id>"

 

Sign-in to your Azure account. https://www.craigforrester.com/posts/azure-cli-basics-logging-in/

az login

 

If you need to use Azure PowerShell CLI

Connect-AzAccount

 

If you need to use the deprecated CLI

Connect-AzureRmAccount

 

To view info on signed in account.


roger_r_cruz@Azure:~$ az ad signed-in-user show { "accountEnabled": true, "ageGroup": null, "assignedLicenses": [], "assignedPlans": [], "city": null, "companyName": null, "consentProvidedForMinor": null, "country": null, "createdDateTime": "2021-12-25T22:52:46Z", "creationType": "Invitation", "deletionTimestamp": null, "department": null, "dirSyncEnabled": null, "displayName": "roger_r_cruz", "employeeId": null, "facsimileTelephoneNumber": null, "givenName": null, "immutableId": null, "isCompromised": null, "jobTitle": null, "lastDirSyncTime": null, "legalAgeGroupClassification": null, "mail": "roger_r_cruz@yahoo.com", "mailNickname": "roger_r_cruz_yahoo.com#EXT#", "mobile": null, "objectId": "d6f8b634-f3f5-4f98-abd1-0d224e20bc4e", "objectType": "User", "odata.metadata": "https://graph.windows.net/604c1504-c6a3-4080-81aa-b33091104187/$metadata#directoryObjects/@Element", "odata.type": "Microsoft.DirectoryServices.User", "onPremisesDistinguishedName": null, "onPremisesSecurityIdentifier": null, "otherMails": [ "roger_r_cruz@yahoo.com" ], "passwordPolicies": null, "passwordProfile": null, "physicalDeliveryOfficeName": null, "postalCode": null, "preferredLanguage": null, "provisionedPlans": [], "provisioningErrors": [], "proxyAddresses": [ "SMTP:roger_r_cruz@yahoo.com" ], "refreshTokensValidFromDateTime": "2021-12-25T22:52:46Z", "showInAddressList": false, "signInNames": [], "sipProxyAddress": null, "state": null, "streetAddress": null, "surname": null, "telephoneNumber": null, "thumbnailPhoto@odata.mediaEditLink": "directoryObjects/d6f8b634-f3f5-4f98-abd1-0d224e20bc4e/Microsoft.DirectoryServices.User/thumbnailPhoto", "usageLocation": null, "userIdentities": [], "userPrincipalName": "roger_r_cruz_yahoo.com#EXT#@triplecrownlabs.onmicrosoft.com", "userState": "Accepted", "userStateChangedOn": "2021-12-27T00:08:36Z", "userType": "Guest" }

 


Get Subscription ID

 

subId=$(az account show --subscription "" | jq -r '.id')

 


Generate a random number and append it to a string

 

export STORAGE_ACCOUNT_NAME=storestorage$(openssl rand -hex 5)

 

In Bash

let rNum=$RANDOM*$RANDOM

 


Create a template from all resources in a resource group

 

To export all resources in a resource group as it exists right now, use az group export and provide the resource group name.

az group export --name demoGroup

 

You can save a template from a deployment in the deployment history. The template you get is exactly the one that was used for deployment.

az deployment group export --resource-group demoGroup --name demoDeployment

 


List AD Tenants

 

az account tenant list

 


List Operations Supported By An Azure Provider

For a list of operation supported by Azure Event Grid, run the following Azure CLI command:

az provider operation show --namespace Microsoft.EventGrid

 


Identify the Azure region hosting the target resource group

 

Run the following to identify the Azure region hosting the target resource group and its existing resources

 

export RESOURCE_GROUP_NAME_EXISTING='az30314b-labRG' export LOCATION=$(az group list --query "[?name == '${RESOURCE_GROUP_NAME_EXISTING}'].location" --output tsv)

 

 

# Get the resource group and location export RESOURCE_GROUP=$(az group list --query "[0].name" -o tsv) export LOCATION=$(az group show --name $RESOURCE_GROUP -o json| jq -r ".location")

Create A Resource Group Via Azure CLI

 

az group create --name "rcruz-resource-group" --location eastus

Source: Create virtual networks

 


Query / Filter Output

If you have several items in the group list, you can filter the return values by adding a --query option

az group list --query "[?name == '$RESOURCE_GROUP']"

The query is formatted using JMESPath, which is a standard query language for JSON requests. You can learn more about this powerful filter language at JMESPath — JMESPath


List Resource Groups Starting With Name Pattern

 

az group list --query "[?starts_with(name,'az30305a-')]".name --output tsv

 

From Bash, you can pipe the names and delete the resource groups

az group list --query "[?starts_with(name,'az30305a-')]".name --output tsv | xargs -L1 bash -c 'az group delete --name $0 --no-wait --yes'

 

Prints just the name

az webapp list --resource-group ManagedPlatform --query "[?starts_with(name, 'imgapi')].{Name:name}" --output tsv imgapirogercruz

 


Most Recent Query

 

Output the name of the most recently created container registry

az acr list --query "max_by([], &creationDate).name" --output tsv

 

 


Storage Accounts

Create A Storage Account

 

az storage account create --name <name> --resource-group <resource group name> --location eastus --kind StorageV2 --sku Standard_LRS --access-tier Cool

List Storage Account Contents

 

az storage blob list --account-name <your-unique-storage-account-name> --container-name files --query [].{Name:name} --output table

 


Providers

Register A Provider

 

az provider register --namespace 'Microsoft.Insights'

List Status Of A Provider

az provider show -n Microsoft.Insights

 


Regions

 

Identify regions the current subscription can use.

 

az account list-locations --query "[].{name:name}" -o table

 


Create + Publish an Azure Function App

 

az functionapp create --resource-group $RESOURCE_GROUP --consumption-plan-location $LOCATION --name $PRODUCT_FUNCTION_NAME --storage-account $STORAGE_ACCOUNT_NAME --functions-version 2

 

zip -q -r $CURRENT_DIR/<function>.PKG.zip ./* az functionapp deployment source config-zip -g $RESOURCE_GROUP -n $PRODUCT_FUNCTION_NAME --src <function>.PKG.zip