Azure Tag Manipulation using Az

Managing tags with Az

John T. Mills
John T. Mills
Azure Tag Manipulation using Az

In Azure architecture, tags are the foundation of a great subscription. Tags can be leveraged to build out a simple CMDB or a complex setup leveraging Azure Automation. They can be used simply as ad-hoc groupings of resources. In this article, we will list the tags for a subscription and add and remove them.

Connect to an Azure tenant

To connect to a tenant, you will run a cmdlet and you will get popup authentication identical to web browser authentication to portal.azure.com. If multi-factor authentication is configured for the user, it will need to be authenticated against.

 PS> Connect-AzAccount

If you have more than one subscription, you may have joined the wrong one automatically. This can be resolved by switching the subscription context. First, list the possible subscriptions, and then you will be able to change to one of the other options.

 PS>  Get-AzSubscription
 Name     Id                                   TenantId                             ..
 ----     --                                   --------                             --
 Apples   aaaaaaaa-1234-1234-1234-bbbbbbbbbbbb cccccccc-1234-1234-1234-dddddddddddd E.
 Oranges  eeeeeeee-1234-1234-1234-ffffffffffff cccccccc-1234-1234-1234-dddddddddddd E.

 PS> Get-AzSubscription -SubscriptionName "Oranges" | Set-AzContext -Name "Oranges"

 Overwrite existing context 'Apples'
 Are you sure you want to replace existing context 'Apples'?
 [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y


 PS> Get-AzSubscription -SubscriptionName "Oranges" | Set-AzContext -Name "Oranges"

 Overwrite existing context 'Apples'
 Are you sure you want to replace existing context 'Apples'?
 [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y

List what tags are in use in your subscription

There may not be any tags assigned, but it’s better to start with an inventory.

 PS> Get-AzTag
 Name            Count
 ----            -----
 grocery         4    
 orchard         1    
 pharmacy        1    

Remove tags from a hidden resource

That seems like a good list, but pharmacy seems out of place. If we wanted to determine the resource assignment for "pharmacy" we can use Get-AzResource.

 PS> Get-AzResource -TagName "pharmacy"                                                    
 Name              : pharmacydesktop/LinuxDiagnostic
 ResourceGroupName : Pharmacy                       
 ResourceType      : Microsoft.Compute/virtualMachines/extensions
 Location          : westus
 ResourceId        : /subscriptions/eeeeeeee-1234-1234-1234-ffffffffffff/re
 Tags              :
                     Name             Value   
                     ===============  =================
                     pharmacy         Springfield

There looks to be something left over from a misconfiguration. This is a hidden resource type, and difficult to track down in large environments using the portal. We can remove the tag with Set-AzResource.

 PS>  Set-AzResource -Name "pharmacydesktop/LinuxDiagnostic" -ResourceGroupName \
    Pharmacy -Tag @{} -ResourceType "Microsoft.Compute/virtualMachines/extensions"

This @{} tag setting is an empty array. Setting the resource tag array to that value removes all of the tags from the object.

Examples in this post can be found on GitHub