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’
Steps
- Declare Variables
#declare variables
$vmName = “MyVmName”
$tagName = “Builder”
$tagValue = “Sonam2”
$resourceGroupName = “myResourceGroupName”
$subscriptionName = “mySubscriptionName”
$tagExists = $false
$toAddHashTable = @{Name=”$tagName”;Value=”$tagValue”}
$vmName = “MyVmName”
$tagName = “Builder”
$tagValue = “Sonam2”
$resourceGroupName = “myResourceGroupName”
$subscriptionName = “mySubscriptionName”
$tagExists = $false
$toAddHashTable = @{Name=”$tagName”;Value=”$tagValue”}
- Login to Azure Account
#login to azure account
$addAzAccount = Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $subscriptionName
$addAzAccount = Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $subscriptionName
- Fetch Azure VM tag list
#Fetch azure vm tags
$vmTagsOriginal = (Get-AzureRmResource -ResourceGroupName $resourceGroupName -Name $vmName).Tags
$vmTagsOriginal = (Get-AzureRmResource -ResourceGroupName $resourceGroupName -Name $vmName).Tags
- 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 = @{}
}
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 new value in vm tag list
#add tag with updated value in vm tag list
$ignoreOutput = $vmTagsArrayList.Add($toAddHashTable)
$ignoreOutput = $vmTagsArrayList.Add($toAddHashTable)
- 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
$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’
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!
Informative blog. Thank you for sharing with us..
ReplyDeleteMS Azure Online Training
I am glad it helped you.
DeleteThanks for sharing this awesome post.
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course| Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Developer Training in Chennai | Mean Stack Developer Training in Chennai | Full Stack Training | Certification | Full Stack Online Training Course
I am glad it helped you.
Delete