Update tags on Azure Virtual Machines


Hi All,

There is an excellent blog available on adding new tags on Azure Virtual Machines. Adding new tags is simple and direct, but updating existing tags requires little more logic.

So here is my blog solving this problem.

Scenario

In below screenshot, I have one virtual machine with existing tag ‘Builder’. I will be updating its value to ‘Sonam2’ from ‘Sonam’

1

Steps

 

  1. Declare Variables

#declare variables
$vmName = "MyVmName"
$tagName = "Builder"
$tagValue = "Sonam2"
$resourceGroupName = "myResourceGroupName"
$subscriptionName = "mySubscriptionName"
$tagExists = $false
$toAddHashTable = @{Name="$tagName";Value="$tagValue"}

  1. Login to Azure Account

#login to azure account
$addAzAccount = Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $subscriptionName

 

  1. Fetch Azure VM tag list

#Fetch azure vm tags
$vmTagsOriginal = (Get-AzureRmResource -ResourceGroupName $resourceGroupName -Name $vmName).Tags

 

  1. Logic to remove tag with old value from vm tag list

#Logic to remove tag with old value from vm tag list
if($vmTagsOriginal)
{
#validate if tag already exists
[System.Collections.ArrayList]$vmTagsArrayList = $vmTagsOriginal
foreach($tag in $vmTagsArrayList)
{
if($tag.Name -eq $toAddHashTable.Name)
{
$tagExists = $true
break;
}
}
#remove existing tag with old value from tag list
if($tagExists -eq $true)
{
$ignoreOutput = $vmTagsArrayList.Remove($tag)
}
}
else
{
#initiate tag list if no tag exists on vm
$vmTagsArrayList = @{}
}

  1. Add tag with new value in vm tag list

#add tag with updated value in vm tag list
$ignoreOutput = $vmTagsArrayList.Add($toAddHashTable)

 

  1. Update azure vm with updated tag list

#update azure vm with new tag list
$ignoreOutput= Set-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceName $vmName -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $vmTagsArrayList -Force

 

Output

In below screenshot, you can see the value of tag ‘Builder’ has been updated to ‘Sonam2’ from ‘Sonam’

2

Full script

 

#declare variables

$vmName = "MyVmName"

$tagName = "Builder"

$tagValue = "Sonam2"

$resourceGroupName = "myResourceGroupName"

$subscriptionName = "mySubscriptionName"

$tagExists = $false             

$toAddHashTable = @{Name="$tagName";Value="$tagValue"}

 

#login to azure account

$addAzAccount = Login-AzureRmAccount

Select-AzureRmSubscription -SubscriptionName $subscriptionName

 

#Fetch azure vm tags   

$vmTagsOriginal = (Get-AzureRmResource -ResourceGroupName $resourceGroupName -Name $vmName).Tags

 

#Logic to remove tag with old value from vm tag list

if($vmTagsOriginal)

{

    #validate if tag already exists

    [System.Collections.ArrayList]$vmTagsArrayList = $vmTagsOriginal

    foreach($tag in $vmTagsArrayList)

    {           

        if($tag.Name -eq $toAddHashTable.Name)

        {

            $tagExists = $true

            break;

        }

    }

    #remove existing tag with old value from tag list

    if($tagExists -eq $true)

    {

        $ignoreOutput = $vmTagsArrayList.Remove($tag)

    }

}

else

{

    #initiate tag list if no tag exists on vm

    $vmTagsArrayList = @{}

}

 

#add tag with updated value in vm tag list

$ignoreOutput = $vmTagsArrayList.Add($toAddHashTable)

 

#update azure vm with new tag list

$ignoreOutput= Set-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceName $vmName -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $vmTagsArrayList -Force

 

Hope this was helpful. Happy Coding!


Comments (2)

  1. Jason Billingham says:

    Hi there, and thank you so much for this article! This appears to be the ONLY article on the entire web that actually addresses what I’m trying to do, and I can’t get it to work 😀

    I’ve changed the variables in your code, and everything runs fine until it gets to the set-azurermresource command.

    I get “Cannot convert ‘System.Collections.ArrayList’ to the type ‘System.Collections.Hashtable’ required by parameter ‘Tag’. Specified method is not supported.

    Has something changed in the way that Tags are handled since October? This is not the first time I’ve seen this message. I tried as well to do similar to what your script is trying to do, and got to the same error.

    Anyone with any thoughts would be appreciated. I’ve posted the question on MSDN, and I’ve got an Azure Portal ticket open and they keep sending me the same article about how to add a new Tag to a resource. I don’t understand how this is not something that more people are trying to do.

    Thanks!
    Jason

    1. Hi Jason, i am running this code on PowerShell version 5.0 and Azure PowerShell version 1.2.3

      The given script works absolutely fine for me in both cases: adding a new tag + updating an existing tag.
      Try updating your versions if older.

Skip to main content