Azure Tags

You apply tags to your Azure resources to logically organize them by categories. Each tag consists of a name and a value. For example, you can apply the name “Environment” and the value “Production” to all the resources in production.
After you apply tags, you can retrieve all the resources in your subscription with that tag name and value. Tags enable you to retrieve related resources from different resource groups. This approach is helpful when you need to organize resources for billing or management.
 

Problem:

Sometimes tags are applied in environments prior to developing a tagging strategy. The problem in exponentially increased with the size of the environment and the number of users creating resources.
Currently we are looking for a solution to remove specific unwanted tags from Virtual Machines.
For this purpose , the below mentioned script was developed that solves the problem.

Solution :

The below mentioned script performs the following tasks

  • Get the list of all the VMs based on the Scope
  • Get all the Tag Values for each VM in a $VMTags variable.
  • Copy all the values from $VMtags to $newtags except the $tagtoremove value.
  • Configure the resources with the $newtags values using Set-AzureRmResource command.

 

Code:

#Getting the list of VMs based on the resource group. THe Scope can be changed to include more resources.
$VMS = Get-AzureRmVM -ResourceGroupName ResourceGroupName
#Details of the tag to remove are stored in the $TagtoRemove variable.
$TagtoRemove = @{Key="TestVmName";Value="abcd"}
foreach ($VM in $VMs)
{
 $VMtags = $VM.tags # Getting the list of all the tags for the VM.
 $newtag = @{} # Creating a new Hashtable variable to store the Tag Values.
 foreach ( $KVP in $VMtags.GetEnumerator() )
 {
 Write-Host "`n`n`n"
 If($KVP.Key -eq $TagtoRemove.Key)
 {
 write-host $TagtoRemove.Key "exists in the "$VM.Name " will be removed `n"}
 Else
 {
 $newtag.add($KVP.Key,$KVP.Value) # Adding all the tags in the $newtag Variable except the $TagtoRemove.key values
 }
}
 #Updating the Virtual machine with the updated tag values $newtag.
 Set-AzureRmResource -ResourceGroupName $VM.ResourceGroupName -ResourceName $VM.Name -Tag $newtag -Force -ResourceType Microsoft.Compute/VirtualMachines
}

 
 

Category:
Azure Platform, PowerShell, Uncategorized
Tags:
,

Join the conversation! 2 Comments

  1. Thank you for this. Saved me a lot of time!

  2. hi
    i search a powersell command for example:
    Set “Tags” of de public ip addrees azure.

    you can helpme plis?

    for ASM o RMS.

    thks

Comments are closed.