Search code examples
exceltagsazure-powershellazureportalazure-resource-group

Azure Portal - Assign tags to Resource Groups using Powershell and reading data from Excel spreadsheet or CSV


I am looking to apply a new set of tags to a number of Azure Resource Groups. The tags must adhere to a naming convention which is a concatenation of a number of Microsoft Excel column data field values and will be read row by row for each Resource Group.

For example, let's use the below as an example of the first 6 columns:

Column A: Azure Subscription Name Column B: Resource Group Name Column C: Business Unit Column D: Cost Center Column E: Project Code Column F: Service or Application Name

enter image description here

The proposed tag will then be a concatenation of for example:

[Column A] - [Column B] - [Column C] - [Column E] - [Column F]

The resultant tag for the first row should therefore look like the below example:

"Subscription 1 : RG-01 : BU-A1 : 1001 : WebApp-1"

Powershell would probably be the preferred solution to complete this task and I'd welcome any suggestions or ideas on how to achieve this.


Solution

  • Updated answer 0406:

    The scripts below can work on multi-rows scenario:

    Note: to use Set-AzContext cmdlet, please install Az.Accounts 2.2.7 module.

    #load the .csv file
    $file_path = "D:\test\t1.csv"
    
    #define the tag name, you can change it as per your need
    $tag_name="mytag111"
    
    
    #loop the values in the .csv file
    Import-Csv -Path $file_path | ForEach-Object{
    
    #define a tag value with empty string
    $tag_value=""
    
    #define a variable for subscription name
    $subscription_name = ""
    
    #define a variable for resource group name
    $resource_group_name = ""
    
    foreach($property in $_.PSObject.Properties)
    {
    
     #here, get the subscription name from the csv file
     if($property.name -eq "Az Sub Name"){
            $subscription_name = $property.Value
       }
     #here, get the resource group name from the csv file
     if($property.name -eq "RG Name"){
            $resource_group_name = $property.Value
       }
    
      #exclude the "Cost Ctr" column
      if(!($property.name -eq "Cost Ctr"))
      {
        #then we loop all the values from each row, and then concatenate them
        #here, we just don't want to add the colon(:) at the end of the value from "Svc/App" column
        if(!($property.name -eq "Svc/App"))
        {  
        $tag_value += $property.Value + " : "
        }
        else
        {
        $tag_value += $property.Value
        }
      } 
     
    }
    
    #change the context as per different subscription
    Set-AzContext -Subscription $subscription_name
    
    #get the existing tags for the resource group
    $tags = (Get-AzResourceGroup -Name $resource_group_name).Tags
    #add the new tags to the existing tags, so the existing tags will not be removed
    $tags +=@{$tag_name=$tag_value}
    #set the tags
    Set-AzResourceGroup -Name $resource_group_name -Tag $tags
    
    }
    
    "completed********"
    

    Here are the test data:

    enter image description here

    The scripts work fine as per my testing. And please let me know if you have any issues about it.


    Original answer:

    Please correct me if I misunderstood you.

    I write a simple code to read data from .csv file and then add tags to the resource group.

    Note that in my testing, there is only 1 row data in .csv file and hard-cord the resource group name, please feel free to modify the code to meet your requirement.

    And to use the Set-AzResourceGroup cmdlet, you should make sure the Az.Resources module is installed.

    The .csv file:

    enter image description here

    The powershell code:

    #load the .csv file
    $file_path = "D:\test\t1.csv"
    
    #define the tag name, you can change it as per your need
    $tag_name="mytag111"
    #define a tag value with empty string
    $tag_value=""
    
    #loop the values in the .csv file
    Import-Csv -Path $file_path | ForEach-Object{
    
    foreach($property in $_.PSObject.Properties)
    {
      #exclude the "Cost Ctr" column
      if(!($property.name -eq "Cost Ctr"))
      {
        #then we loop all the values from each row, and then concatenate them
        #here, we just don't want to add the colon(:) at the end of the value from "Svc/App" column
        if(!($property.name -eq "Svc/App"))
        {
        $tag_value += $property.Value + " : "
        }
        else
        {
        $tag_value += $property.Value
        }
      } 
     
    }
    
    #get the existing tags for the resource group
    $tags = (Get-AzResourceGroup -Name yyrg11).Tags
    #add the new tags to the existing tags, so the existing tags will not be removed
    $tags +=@{$tag_name=$tag_value}
    #set the tags
    Set-AzResourceGroup -Name yyrg11 -Tag $tags
    
    }
    
    "completed********"
    

    The test result:

    enter image description here

    In azure portal, the tag is added:

    enter image description here