通过 CSV 向 Azure VM 添加标签
Adding tags to azure VMs via CSV
尝试通过 CSV 数据向 VM 添加标签,现在我有以下代码:
if ($context.Account -eq $null) {
# Login-AzureAccount
Connect-AzAccount
}
# Select Azure Subscription
$subscriptionId = (Get-AzSubscription | Out-GridView -Title "Select an Azure Subscription ..." -PassThru).SubscriptionId
#Select specified subscription ID
Select-AzSubscription -SubscriptionId $subscriptionId
$InputCSVFilePath = "test.csv"
$csvItems = Import-Csv $InputCSVFilePath
################
foreach ($item in $csvItems){
Clear-Variable r
#$r = Get-AzResource -ResourceGroupName $item.ResourceGroup -Name $item.VM -ErrorAction Continue
$r = Get-AzResource -Name $item.VM
################
if ($r -ne $null){
if ($r.Tags){
# Tag - Client DL
if ($r.Tags.ContainsKey("Client_DL")){
$r.Tags["Client_DL"] = $item.ClientDL
}else{
$r.Tags.Add("Client_DL", $item.ClientDL)
}
# Tag - Priority
if ($r.Tags.ContainsKey("Priority")){
$r.Tags["Priority"] = $item.Priority
}else{
$r.Tags.Add("Priority", $item.Priority)
}
}
}else{
Write-Host "No VM found named $($item.VMName)!"
}
}
我确认我的代码确实通过了函数,但由于某些原因,我的 VM 上没有设置标签。我 运行 在 powershell 中手动执行命令,我可以通过以下方式设置标签:
$r = Get-AzResource -Name TestVM
$r.Tags.Add("Client_DL", "TEST-DL")
我错过了什么吗?我是 运行 一个 Set-PSDebug -Trace 2 当 运行 我的代码时,它似乎检查得很好,但标签没有得到 set/written.
所以您在内存中添加了标签,但没有调用任何 Az cmdlet 将标签重新设置到资源上。
您可以在文档 here 的示例中看到他们 return 使用 Get-AzResource
的 VM,将他们的新标签附加到现有标签然后使用 Set-AzResource
写回新添加的标签。
注意一点
When updating tags through PowerShell, tags are updated as a whole. If you are adding one tag to a resource that already has tags, you will need to include all the tags that you want to be placed on the resource
或者您可以使用 Update-AzTag
,它有一个 -Operation
参数,让您选择是否要合并、替换或删除现有标签。
https://docs.microsoft.com/en-us/powershell/module/az.resources/update-aztag?view=azps-6.0.0
最终,您需要将 $r.Tags
值设置回您的资源,作为 if
语句中的最后一个操作。
尝试通过 CSV 数据向 VM 添加标签,现在我有以下代码:
if ($context.Account -eq $null) {
# Login-AzureAccount
Connect-AzAccount
}
# Select Azure Subscription
$subscriptionId = (Get-AzSubscription | Out-GridView -Title "Select an Azure Subscription ..." -PassThru).SubscriptionId
#Select specified subscription ID
Select-AzSubscription -SubscriptionId $subscriptionId
$InputCSVFilePath = "test.csv"
$csvItems = Import-Csv $InputCSVFilePath
################
foreach ($item in $csvItems){
Clear-Variable r
#$r = Get-AzResource -ResourceGroupName $item.ResourceGroup -Name $item.VM -ErrorAction Continue
$r = Get-AzResource -Name $item.VM
################
if ($r -ne $null){
if ($r.Tags){
# Tag - Client DL
if ($r.Tags.ContainsKey("Client_DL")){
$r.Tags["Client_DL"] = $item.ClientDL
}else{
$r.Tags.Add("Client_DL", $item.ClientDL)
}
# Tag - Priority
if ($r.Tags.ContainsKey("Priority")){
$r.Tags["Priority"] = $item.Priority
}else{
$r.Tags.Add("Priority", $item.Priority)
}
}
}else{
Write-Host "No VM found named $($item.VMName)!"
}
}
我确认我的代码确实通过了函数,但由于某些原因,我的 VM 上没有设置标签。我 运行 在 powershell 中手动执行命令,我可以通过以下方式设置标签:
$r = Get-AzResource -Name TestVM
$r.Tags.Add("Client_DL", "TEST-DL")
我错过了什么吗?我是 运行 一个 Set-PSDebug -Trace 2 当 运行 我的代码时,它似乎检查得很好,但标签没有得到 set/written.
所以您在内存中添加了标签,但没有调用任何 Az cmdlet 将标签重新设置到资源上。
您可以在文档 here 的示例中看到他们 return 使用 Get-AzResource
的 VM,将他们的新标签附加到现有标签然后使用 Set-AzResource
写回新添加的标签。
注意一点
When updating tags through PowerShell, tags are updated as a whole. If you are adding one tag to a resource that already has tags, you will need to include all the tags that you want to be placed on the resource
或者您可以使用 Update-AzTag
,它有一个 -Operation
参数,让您选择是否要合并、替换或删除现有标签。
https://docs.microsoft.com/en-us/powershell/module/az.resources/update-aztag?view=azps-6.0.0
最终,您需要将 $r.Tags
值设置回您的资源,作为 if
语句中的最后一个操作。