Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

Apply Tags on Azure resources

Posted on November 7, 2019February 27, 2022

Update : updated script to support Azure Az powershell module.

In my previous blog, I talked about benefits of tagging and how to tag resource groups using powershell. A script was also provided with some standard example tags – like, Environment, Budget Category, Application etc. Let’s keep on working on the same situation like last blog. We have hundreds of resource groups and each resource group has one more resources associated with it. We already tagged the resource groups and now planning to tag those resources (whatever, resource can accept tags. Not all Azure resource type can be tagged.).

Again, Azure Resource Manager Policy should be our first choice of method as it is the best way to keep those resources compliant for tags. However, as I did in the previous blog, I will use Powershell to tag resources. This time, I am not using a csv file as input. It will be too much to parse through such a huge number of rows and tag based on those.It can be done, but, this time I decided to use the tags associated with resource groups will be applied to Azure resources with in that resource group. I am assuming, all resources with in the resource group are deployed for same Environment, Application , Department or Budget Category etc. as the resource group.

I think, we have enough context. Let’s start scripting.

Make sure you have Azure Az Module installed and imported on the system before running this script. For more information on how to install and configure Az module refer following article : https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.5.0

First, login to your Azure account with Login-AzAccount in Powershell console. Enter your credentials when prompted.

Now, the script run Get-AzResourceGroup on the subscription you have set your context and running through a loop. In every iteration, it gets resource group name and all the associated tags with the resource group. After that, it finds all resources within the resource group with Get-AzResource. Loop through each resource and checks if it already has those tags. If it does not have those tags already, script will apply those missing tags taken from the resource group. Here is the complete script do these all.

Get-AzResourceGroup | Select-Object ResourceGroupName,Tags | ForEach-Object{
	$rgName = $rgTags = $null
	$rgName = $_.ResourceGroupName
	$rgTags = $_.Tags
	Get-AzResource -ResourceGroupName $rgName | Select-Object Name,Tags,ResourceId | ForEach-Object{
		$resName = $resTags = $resId = $null
		$resName = $_.Name
		$resTags = $_.Tags
		$resId = $_.ResourceId
		if([String]::IsNullOrEmpty($resTags)){
			$resTags = @{}
			if($rgTags.Keys -contains "TeamName"){$resTags.Add("TeamName", $rgTags.TeamName)}
			if($rgTags.Keys -contains "BudgetCategory"){$resTags.Add("BudgetCategory", $rgTags.BudgetCategory)}
			if($rgTags.Keys -contains "Program"){$resTags.Add("Program", $rgTags.Program)}
			if($rgTags.Keys -contains "Environment"){$resTags.Add("Environment", $rgTags.Environment)}
			if($rgTags.Keys -contains "Department"){$resTags.Add("Department", $rgTags.Department)}
		}
		else{
			if($resTags.Keys -notcontains "TeamName"){
				if($rgTags.Keys -contains "TeamName"){
					$resTags.Add("TeamName", $rgTags.TeamName)
				}
			}
			if($resTags.Keys -notcontains "BudgetCategory"){
				if($rgTags.Keys -contains "BudgetCategory"){
					$resTags.Add("BudgetCategory", $rgTags.BudgetCategory)
				}			
			}
			if($resTags.Keys -notcontains "Program"){
				if($rgTags.Keys -contains "Program"){
					$resTags.Add("Program", $rgTags.Program)
				}			
			}
			if($resTags.Keys -notcontains "Environment"){
				if($rgTags.Keys -contains "Environment"){
					$resTags.Add("Environment", $rgTags.Environment)
				}			
			}
			if($resTags.Keys -notcontains "Department"){
				if($rgTags.Keys -contains "Department"){
					$resTags.Add("Department", $rgTags.Department)
				}			
			}		
		}
		Set-AzResource -Tag $resTags -ResourceId $resId -Force
	}
}

That’s it! Now you have applied tags to all resources which can be tagged. In case you are either not interested to tag resources in every resource group or just want to do it in parts, you can create a file containing all those target resource groups and run a loop through it like Get-AzResourceGroup -ResourceGroupName <name of resource group>.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes