How can I change just the name of Azure TAG by not touching the value. can this be achieved by Policy

vaibhavshete 0 Reputation points
2024-02-08T13:21:35.9633333+00:00

How can I change just the name of Azure TAG by not touching the value. can this be achieved by Policy ?? Has anyone done this??

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
799 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Luis Arias 5,046 Reputation points
    2024-02-08T21:27:53.0466667+00:00

    Hi vaibhavshete, I think the best option to accomplish this task is by CLI, so You can loop on your resources get the OldTag , create NewTag with the value of the OldTag and finally remove the OldTag. An example on powershell could be:

    # Get the resource and tags
    $resource = Get-AzResource -Name "RESOURCE_NAME" -ResourceGroupName "RESOURCE_GROUP_NAME"
    $tags = $resource.Tags
    
    # Check old tag
    if ($tags.ContainsKey("OLD_TAG_NAME")) {
        # Store the value of the old tag
        $tagValue = $tags["OLD_TAG_NAME"]
        # Create new tag with old value
        $tags["NEW_TAG_NAME"] = $tagValue
        # Update resource with new tags
        $resource | Set-AzResource -Tag $tags -Force
        # Remove old tag key
        $tags.Remove("OLD_TAG_NAME")
        # Update resource without old tag
        $resource | Set-AzResource -Tag $tags -Force
    }
    
    

    Let me know if this help you. Luis

    0 comments No comments