az login

az account list # Lists all the available Azure accounts
az account show # Shows the `isDefault` account
az account set --subscription myCompanyCommerceDevTest # Sets the `isDefault` account subscription
az account set --subscription "myCompanyCommerce"
az account set --subscription "myCompanyCommerceDevTest"
az ad sp list --display-name myCompanyCommerceSPN
az monitor activity-log list --correlation-id <id>
as tag add-value
az monitor activity-log list

Azure CLI to deplay ARM Templates

Select the correct subscription:

az account set --subscription "mySubscription"

Test a Deployment template

az group deployment validate \
  --resource-group ExampleGroup \
  --template-file storage.json \
  --parameters @storage.parameters.json

Then deploy

az group deployment create \
  --name ExampleDeployment \
  --resource-group ExampleGroup \
  --template-file storage.json \
  --parameters @storage.parameters.json
az resource list \
     --query "[?contains('"Microsoft.Compute/virtualMachines","Microsoft.Sql/servers/databases","Microsoft.HDInsight/clusters","Microsoft.DocumentDb/databaseAccounts","Microsoft.Web/serverfarms"', type) && tags.SpinUpSpinDownSchedule== 'Excluded'].{ResourceGroup:resourceGroup, Type:type, Name:name}" \
     --output table

Tags in Azure

az tag add-value
az tag list --subscription
az resource tag --tags vmlist=vm1 --ids /subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/Microsoft.Web/sites/{WebApp}

az resource show --id <resource-id> --query tags

az resource tag --tags 'Dept=IT' 'Environment=Test' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"


Applying tags to resource groups and then to all contained resources in the Azure CLI is shown here

It would certainly be worth while to look at all our resources and ensurfe thay they are tagged consistantly.

Q: Can we show all resources with a given key/value pair? This could be used to remove the stupid space in “Price & Promo” etc Beware though as new deployments are likely to override this.

Neither of these scripts handle spaces in tag names or values.

jsontags=$(az group show --name examplegroup --query tags -o json)
tags=$(echo $jsontags | tr -d '"{},' | sed 's/: /=/g')
resourceids=$(az resource list -g examplegroup --query [].id --output tsv)
for id in $resourceids
do
  az resource tag --tags $tags --id $id
done

and to maintain existing tags use…

jsontags=$(az group show --name examplegroup --query tags -o json)
tags=$(echo $jsontags | tr -d '"{},' | sed 's/: /=/g')

resourceids=$(az resource list -g examplegroup --query [].id --output tsv)
for id in $resourceids
do
  resourcejsontags=$(az resource show --id $id --query tags -o json)
  resourcetags=$(echo $resourcejsontags | tr -d '"{},' | sed 's/: /=/g')
  az resource tag --tags $tags$resourcetags --id $id
done

SPNs

az account list
az account set --subscription CommerceDevTest
az ad sp list --display-name CommerceDevTestSPN > CommerceDevTestSPN.json