Friday 2 June 2017

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 exist
[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 of 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 exist
    [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 of 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!

4 comments: