Актуализиран отговор 0406:
Скриптовете по-долу могат да работят при сценарий с няколко реда:
Забележка: за използване на Set-AzContext cmdlet, моля, инсталирайте Az.Accounts 2.2. 7 модул.
#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 файл и след това добавям тагове към групата ресурси.
Имайте предвид, че при моето тестване има само 1 ред данни в .csv файл и твърдо свържете името на групата ресурси, моля, не се колебайте да промените кода, за да отговаря на вашите изисквания.
И да използвате Set-AzResourceGroup cmdlet, трябва да се уверите, че 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 се добавя етикетът:
person
Ivan Yang
schedule
02.04.2021