Use Case: Update all the Azure Resource Group Tags within a select Subscription without overwriting the existing tags names and values from a CSV file.
Step by Step Guide to Update Resource Group Tags:
Step 1: Export all the existing Resource Group Tags to a CSV File for each subscription using PowerShell.
$Subscription="_add_Subscriptionn_name" Set-AzContext -SubscriptionName "$Subscription" # Initialise output array $Output = @() # Collect all the groups from the current subscription $ResourceGroups = Get-AzResourceGroup # Obtain a unique list of tags for these groups collectively $UniqueTags = $ResourceGroups.Tags.GetEnumerator().Keys | Sort-Object -Unique # Loop through the resource groups foreach ($ResourceGroup in $ResourceGroups) { # Create a new ordered hashtable and add the normal properties first. $RGHashtable = [ordered] @{} $RGHashtable.Add("Name",$ResourceGroup.ResourceGroupName) $RGHashtable.Add("Location",$ResourceGroup.Location) # Loop through possible tags adding the property if there is one, adding it with a hyphen as it's value if it doesn't. if ($ResourceGroup.Tags.Count -ne 0) { $UniqueTags | Foreach-Object { if ($ResourceGroup.Tags[$_]) { $RGHashtable.Add("$_",$ResourceGroup.Tags[$_]) } else { $RGHashtable.Add("$_","-") } } } else { $UniqueTags | Foreach-Object { $RGHashtable.Add("$_","-") } } # Update the output array, adding the ordered hashtable we have created for the ResourceGroup details. $Output += New-Object psobject -Property $RGHashtable } # Sent the final output to CSV $Output | Export-Csv -Path "./_add_Subscriptionn_name-RGTagsList.csv" -NoClobber -NoTypeInformation -Encoding UTF8 -Force
Step 2: Open the downloaded CSV file and the modify, validate all the Azure Resource Group Tag Names and Values directly in a CSV file. Also remove the unnecessary Tag Name columns and values.
Step 3: Once validated Upload CSV file to Azure Cloud Shell (Name it as subName-RGTagsList.csv)
Step 4: Run the below PowerShell script to Update all the Resource Group Tags from an updated CSV file.
$InputCSVFile = Import-CSV "./_add_Subscriptionn_name-RGTagsList.csv" $Subscription="_add_Subscriptionn_name" Set-AzContext -SubscriptionName "$Subscription" $ResourceGroups=Get-AzResourceGroup foreach ($RG in $ResourceGroups){ $RGid=$RG.ResourceId $RGName = $RG.ResourceGroupName #Pre Validation Check Write-Output "$RGid - $RGName" foreach($line in $InputCSVFile){ if ($RGName -eq $line.ResourceName) { #Pre Validation Check Write-Output "$RGName" $mergedTags = @{ ApplicationName=$line.ApplicationName; BusinessArea=$line.BusinessArea; ChargeBackTo=$line.ChargeBackTo; CostCenter=$line.CostCenter; DataType=$line.DataType; Environment=$line.Environment; GDPRData=$line.GDPRData; ITTeam=$line.ITTeam; ManagedBy=$line.ManagedBy; OwnerEmail=$line.OwnerEmail; OwnerName=$line.OwnerName; PersonalData=$line.PersonalData; PersonalHealthData=$line.PersonalHealthData; Solution=$line.Solution; SystemCriticality=$line.SystemCriticality } Update-AzTag -ResourceId $RGid -Tag $mergedTags -Operation Merge Write-Output "---------------"#> } } }
Step 5: Once done verify tags on Azure Resource Groups.