Azure 门户 - 使用 Powershell 为资源组分配标签并从 Excel 电子表格或 CSV 中读取数据
Azure Portal - Assign tags to Resource Groups using Powershell and reading data from Excel spreadsheet or CSV
我希望将一组新标签应用到多个 Azure 资源组。标签必须遵守命名约定,该约定是许多 Microsoft Excel 列数据字段值的串联,并且将逐行读取每个资源组。
例如,我们以下面的前6列为例:
A 列:Azure 订阅名称
B 列:资源组名称
C 列:业务部门
D 列:成本中心
E 栏:项目代码
F 列:服务或应用程序名称
提议的标签将是以下内容的串联:
[A 列]-[B 列]-[C 列]-[E 列]-[F 列]
因此,第一行的结果标记应如下例所示:
“订阅 1:RG-01:BU-A1:1001:WebApp-1”
Powershell 可能是完成此任务的首选解决方案,我欢迎就如何实现此任务提出任何建议或想法。
更新答案 0406:
以下脚本适用于多行场景:
注意:使用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********"
测试数据如下:
根据我的测试,脚本运行良好。如果您对此有任何问题,请告诉我。
原回答:
如果理解有误请指正
我写了一个简单的代码来从.csv文件中读取数据,然后将标签添加到资源组中。
注意在我的测试中,.csv文件中只有1行数据并且硬编码了资源组名称,请随意修改代码以满足您的需求要求。
并安装了 Set-AzResourceGroup cmdlet, you should make sure the Az.Resources 模块。
.csv 文件:
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, 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********"
测试结果:
在azure portal中,添加标签:
我希望将一组新标签应用到多个 Azure 资源组。标签必须遵守命名约定,该约定是许多 Microsoft Excel 列数据字段值的串联,并且将逐行读取每个资源组。
例如,我们以下面的前6列为例:
A 列:Azure 订阅名称 B 列:资源组名称 C 列:业务部门 D 列:成本中心 E 栏:项目代码 F 列:服务或应用程序名称
提议的标签将是以下内容的串联:
[A 列]-[B 列]-[C 列]-[E 列]-[F 列]
因此,第一行的结果标记应如下例所示:
“订阅 1:RG-01:BU-A1:1001:WebApp-1”
Powershell 可能是完成此任务的首选解决方案,我欢迎就如何实现此任务提出任何建议或想法。
更新答案 0406:
以下脚本适用于多行场景:
注意:使用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********"
测试数据如下:
根据我的测试,脚本运行良好。如果您对此有任何问题,请告诉我。
原回答:
如果理解有误请指正
我写了一个简单的代码来从.csv文件中读取数据,然后将标签添加到资源组中。
注意在我的测试中,.csv文件中只有1行数据并且硬编码了资源组名称,请随意修改代码以满足您的需求要求。
并安装了 Set-AzResourceGroup cmdlet, you should make sure the Az.Resources 模块。
.csv 文件:
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, 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********"
测试结果:
在azure portal中,添加标签: