Azure 门户 - 使用 Powershell 为资源组分配标签并从 Excel 电子表格或 CSV 读取数据

问题描述

我希望将一组新标签应用于多个 Azure 资源组。标记必须遵守命名约定,该约定是多个 Microsoft Excel 列数据字段值的串联,并且将为每个资源组逐行读取。

例如,让我们使用以下作为前 6 列的示例:

A 列:Azure 订阅名称 B 列:资源组名称 C栏:业务单位 D 列:成本中心 E 栏:项目代码 F 列:服务或应用程序名称

enter image description here

建议的标签将是例如以下内容的串联:

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

因此,第一行的结果标签应如下例所示:

订阅 1:RG-01:BU-A1:1001:WebApp-1”

Powershell 可能是完成这项任务的首选解决方案,我欢迎任何关于如何实现这一目标的建议或想法。

解决方法

更新答案 0406:

以下脚本适用于多行场景:

注意:要使用 Set-AzContext cmdlet,请安装 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********"

以下是测试数据:

enter image description here

根据我的测试,脚本运行良好。如果您有任何问题,请告诉我。


原答案:

如果我误解了你,请纠正我。

我编写了一个简单的代码来从 .csv 文件中读取数据,然后向资源组添加标签。

注意在我的测试中,.csv文件中只有1行数据并且硬编码了资源组名称,请随时修改代码以满足您的要求。

要使用 Set-AzResourceGroup cmdlet,您应该确保安装了 Az.Resources 模块。

.csv 文件:

enter image description here

powershell 代码:

#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,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********"

测试结果:

enter image description here

在 azure 门户中,添加了标签:

enter image description here