Tags are one of Microsoft Azure's most useful organizational features. Tags allow you to organize resources across nearly all Azure services in user-specific ways. Common uses of tags are for billing purposes, grouping Azure resources by department, and more. One way to manage tags is via PowerShell. Managing tags with PowerShell allows you to add, remove, and manage tags in your automation scripts.

Before we get too far, you're first going to need the Azure Resource Manager (AzureRM) PowerShell module. This module is available in the PowerShell Gallery. You can download and install it using the Install-Module -Name AzureRm command. Once you have the AzureRM PowerShell module, you're good to go!

If you aren't already authenticated to Azure, be sure to do so using Connect-AzureRmAccount. After authenticating, you're now ready to begin working with Azure tags.

The first task I recommend doing is checking to see if you have any existing tags created using the Get-AzureRmTag cmdlet. The Get-AzureRmTag command lets us retrieve all the tags currently created within the Azure subscription we're connected to.

The screenshot below shows that you can run the Get-AzureRmTag cmdlet as is with no parameters. This only returns limited data, or you can use the Detailed parameter. The Detailed parameter has a ValuesTable property that contains all the interesting information we're looking for. In the screenshot below, expanding the ValuesTable property shows we have a few tags already created.

Using Get AzureRmTag

Using Get AzureRmTag

It looks like we don't have tags assigned to VMs, but I know there are some VMs in this subscription. I've currently got a couple of VMs called TEST-DC and PROD-DC. Perhaps different departments control these VMs, and I'd like to tag them to run reports on them later. To do this, I can first query the VMs using the Get-AzureRmVm cmdlet.

$prodVm = Get-AzureRmVM -Name 'PROD-DC' -ResourceGroupName 'VMs'
$testVm = Get-AzureRmVM -Name 'TEST-DC' -ResourceGroupName 'VMs'

Once I have captured the VMs in Azure objects, I can then more easily pass the required parameter values to the Set-AzureRmResource command, which has the Tag parameter.

To assign tags, the Set-AzureRmResource cmdlet requires the resource group name, resource type, name of the resource, and the tag in hash table format. Using the VM objects' properties, I can then specify all the required parameter values.

Set-AzureRmResource -ResourceGroupName $prodVm.ResourceGroupName ‑ResourceType $prodVm.Type -ResourceName $prodVm.Name -Tag @{ Department = 'HR' } -Force
Set-AzureRmResource -ResourceGroupName $testVm.ResourceGroupName ‑ResourceType $testVm.Type -ResourceName $testVm.Name -Tag @{ Department = 'IT' } -Force
Setting Azure tags

Setting Azure tags

At this point, each VM resource has a Department tag of HR or IT. We can confirm this by running Get-AzureRmResource and looking at the Tags property.

Get-AzureRmResource -ResourceGroupName $prodVm.ResourceGroupName  ResourceName $prodVm.Name -ResourceType $prodVm.Type | Select-Object  ExpandProperty Tags

Key        Value
---        -----
Department HR

Although the Set-AzureRmResource command does a great job at adding tags, it doesn't do a great job at keeping existing ones! If you're not careful, you'll find Set-AzureRmResource actually replaces any existing tags your resource has! Azure resources can have multiple tags assignable by adding a "tag object" to the Tags property within a resource.

For example, perhaps I now want to add a Billing tag to my PROD-DC VM. I can do this by first gathering the Azure resource object.

$vm = Get-AzureRmResource -ResourceGroupName $prodVm.ResourceGroupName ‑ResourceName $prodVm.Name -ResourceType $prodVm.Type

Once I have the resource object, I can then modify its Tags property object using the Add() method.

$vm.Tags.Add('Billing', 'Critical')

Once I have added the tags, I'll then need to commit the new tag set using Set-AzureRmResource.

Set-AzureRmResource -Tag $vm.Tags -ResourceId $vm.ResourceId -Force

After doing this, you'll then see that the VM object now has two tags associated with it.

Subscribe to 4sysops newsletter!

Get-AzureRmResource -ResourceGroupName $prodVm.ResourceGroupName  ResourceName $prodVm.Name -ResourceType $prodVm.Type | Select-Object  ExpandProperty Tags

Key        Value
---        -----
Billing    Critical
Department HR

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2021


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account