如何使用 PowerShell 向现有的 azure 资源添加标签

时间:2021-07-28 16:49:20

标签: azure powershell azure-cli

我通过门户网站在 azure 中创建了大量资源。如何使用 PowerShell 或 CLI 将标签添加到每个现有资源?

2 个答案:

答案 0 :(得分:4)

AZURE POWERSHELL 要添加新标签,请使用 New-AzTag

New-AzTag -ResourceId $resource.id -Tag $tags

要向已有标签的资源添加标签,请使用 Update-AzTag

Update-AzTag -ResourceId $resource.id -Tag

AZURE CLI az 标记 create 替换资源、资源组或订阅上的所有标记。

az tag create --resource-id $resource --tags

要向已有标签的资源添加标签,请使用 az tag update。

az tag update --resource-id $resource --operation Merge --tags Dept=Finance Status=Normal

document 为您提供更多详细信息。

答案 1 :(得分:2)

您可以使用 PowerShell 脚本将标签更新为空资源

connect-azaccount 
$resources=Get-AzResource | Where-Object Tags -eq $null  ##list all the resource whose tags are empty
$resources=Get-AzResourceGroup | Where-Object Tags -eq $null ##list all the resources at resource group level whose tags are empty
$resources | ForEach-Object { New-AzTag -Tag @{ "Env"="test" } -ResourceId $_.ResourceId }