Update : updated script to support Azure Az powershell module.
Many of you can relate to a situation when you have a large cloud environment to manage with hundreds or thousands of resources, however, no way to organize them as those were not set up with proper naming convention or tagging .
Tagging is one of basic yet critical Cloud Governance requirement that every organization should follow while managing resources in cloud.
Tags help us with a way to organize Azure resources in a defined way. This could be based on Environment, Product, Business Unit, Department or even a tag representing some Budget or Cost center if your Organization plans to do a charge back from the users of those resources. Tags are just Key-Value pairs with meaning full entries. Microsoft provides several documents on how to set tags on Azure resources or resource groups. Please refer here to get some good idea on Azure tagging.
You can apply resource tags in many different ways, including through Portal, CLI, Powershell, API, Infrastructure as Code solutions like ARM Templates or Terraform etc. or using Azure Resource Manager Policy. Microsoft provides a list of policies to either do Audit or Apply tags based on certain standard. For may of us Azure Policy will be the first choice to tag resources or resource groups in Azure. I will provide some examples of such policies in future blogs.
Today, I am going to share a script which will help us tagging Azure Resource Groups with a list of Tags and their values provided in an input csv file. However, this can not be an excuse of not just using Policy.
So, 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.
Define a variable to the path of your source csv file with the list of Resource Groups and the Tag names and their values as shown below.
$iFile = "<some dir>\<another dir>\resource_groups.csv"
if(!(Test-Path $iFile)){
Write-Host "File does not exist in the provided path.Try again with correct file path!"
Exit
}
After that, we will now import the csv file and parse through each row. We will also find out if the resource group already has tags applied or not. If there is no tag applied, it is straight forward – just get all the headers from the csv file and set those as tag keys and corresponding cell entries as tag values. However, if resource groups already got some tags applied, script will check for the value.If it does not match with the value defined in the csv file, it will replace with correct value taken from the file. So, enough explanation, let’s write the rest of the script here:
ForEach($rg in import-csv $iFile){
$rgTags = @{}
Write-Host "Setting Tags for Resource Group : $($rg.ResourceGroupName)"
$rgTags = (Get-AzResourceGroup -Name $rg.ResourceGroupName).Tags
if([String]::IsNullOrEmpty($rgTags)){
$rgTags = @{}
$rgTags.Add("TeamName", $rg.TeamName)
$rgTags.Add("BudgetCategory", $rg.BudgetCategory)
$rgTags.Add("Program", $rg.Program)
$rgTags.Add("Environment", $rg.Environment)
$rgTags.Add("Department", $rg.Department)
}
else{
if($rgTags.Keys -contains "TeamName"){
if($rg.TeamName -ne $rgTags["TeamName"]){
$rgTags.Remove("TeamName")
$rgTags.Add("TeamName", $rg.TeamName)
}
}else{$rgTags.Add("TeamName", $rg.TeamName)}
if($rgTags.Keys -contains "BudgetCategory"){
if($rg.BudgetCategory -ne $rgTags["BudgetCategory"]){
$rgTags.Remove("BudgetCategory")
$rgTags.Add("BudgetCategory", $rg.BudgetCategory)
}
}else{$rgTags.Add("BudgetCategory", $rg.BudgetCategory)}
if($rgTags.Keys -contains "Program"){
if($rg.Program -ne $rgTags["Program"]){
$rgTags.Remove("Program")
$rgTags.Add("Program", $rg.Program)
}
}else{$rgTags.Add("Program", $rg.Program)}
if($rgTags.Keys -contains "Environment"){
if($rg.Environment -ne $rgTags["Environment"]){
$rgTags.Remove("Environment")
$rgTags.Add("Environment", $rg.Environment)
}
}else{$rgTags.Add("Environment", $rg.Environment)}
if($rgTags.Keys -contains "Department"){
if($rg.Department -ne $rgTags["Department"]){
$rgTags.Remove("Department")
$rgTags.Add("Department", $rg.Department)
}
}else{$rgTags.Add("Department", $rg.Department)}
}
Set-AzResourceGroup -Tag $rgTags -Name $rg.ResourceGroupName
}
Run the script and check if all your resource groups received those tags defined in the csv file.
Left your comment in the blog if you find any issue in the script. Till then, Happy Scripting!