你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure PowerShell 应用标签

本文介绍如何使用 Azure PowerShell 标记资源、资源组和订阅。 有关标记建议和限制,请参阅 使用标记来组织 Azure 资源和管理层次结构

添加标签

Azure PowerShell 提供两个用于应用标记的命令: New-AzTagUpdate-AzTag。 需要具有 Az.Resources 模块 1.12.0 或更高版本。 您可以使用 Get-InstalledModule -Name Az.Resources 检查您的版本。 可以安装该模块或 安装 Azure PowerShell 3.6.1 或更高版本。

New-AzTag 替换资源、资源组或订阅中的所有标记。 调用命令时,传递要标记的实体的资源 ID。

以下示例将一组标记应用于存储帐户:

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
New-AzTag -ResourceId $resource.id -Tag $tags

命令完成后,请注意资源有两个标记。

Properties :
        Name    Value
        ======  =======
        Dept    Finance
        Status  Normal

如果再次运行该命令,但这次使用不同的标记,请注意早期标记会消失。

$tags = @{"Team"="Compliance"; "Environment"="Production"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
New-AzTag -ResourceId $resource.id -Tag $tags
Properties :
        Name         Value
        ===========  ==========
        Environment  Production
        Team         Compliance

若要将标记添加到已具有标记的资源,请使用 Update-AzTag。 将 -Operation 参数设置为 Merge

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge

请注意,现有标记随着添加两个新标记而增长。

Properties :
        Name         Value
        ===========  ==========
        Status       Normal
        Dept         Finance
        Team         Compliance
        Environment  Production

每个标记名称只能有一个值。 如果为标记提供新值,则即使使用合并作,它也会替换旧值。 以下示例将 Status 标记从 Normal 更改为 Green

$tags = @{"Status"="Green"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge
Properties :
        Name         Value
        ===========  ==========
        Status       Green
        Dept         Finance
        Team         Compliance
        Environment  Production

将参数-Operation设置为Replace时,新标记集将替换现有标记。

$tags = @{"Project"="ECommerce"; "CostCenter"="00123"; "Team"="Web"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Replace

只有新标记保留在资源上。

Properties :
        Name        Value
        ==========  =========
        CostCenter  00123
        Team        Web
        Project     ECommerce

相同的命令也适用于资源组或订阅。 将它们传入要标记的资源组或订阅的标识符。

若要向资源组添加新的标记集,请使用:

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$resourceGroup = Get-AzResourceGroup -Name demoGroup
New-AzTag -ResourceId $resourceGroup.ResourceId -tag $tags

若要更新资源组的标记,请使用:

$tags = @{"CostCenter"="00123"; "Environment"="Production"}
$resourceGroup = Get-AzResourceGroup -Name demoGroup
Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge

若要向订阅添加新的标记集,请使用:

$tags = @{"CostCenter"="00123"; "Environment"="Dev"}
$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
New-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags

若要更新订阅的标记,请使用:

$tags = @{"Team"="Web Apps"}
$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Update-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags -Operation Merge

资源组中可能有多个具有相同名称的资源。 在这种情况下,可以使用以下命令设置每个资源:

$resource = Get-AzResource -ResourceName sqlDatabase1 -ResourceGroupName examplegroup
$resource | ForEach-Object { Update-AzTag -Tag @{ "Dept"="IT"; "Environment"="Test" } -ResourceId $_.ResourceId -Operation Merge }

列出标记

若要获取资源、资源组或订阅的标记,请使用 Get-AzTag 命令并传递实体的资源 ID。

若要查看资源的标记,请使用:

$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Get-AzTag -ResourceId $resource.id

若要查看资源组的标记,请使用:

$resourceGroup = Get-AzResourceGroup -Name demoGroup
Get-AzTag -ResourceId $resourceGroup.ResourceId

若要查看订阅的标记,请使用:

$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Get-AzTag -ResourceId "/subscriptions/$subscription"

按标记列出

若要获取具有特定标记名称和值的资源,请使用:

(Get-AzResource -Tag @{ "CostCenter"="00123"}).Name

若要获取具有特定标签名称且具有任意标签值的资源,请使用:

(Get-AzResource -TagName "Dept").Name

若要获取具有特定标记名称和值的资源组,请使用:

(Get-AzResourceGroup -Tag @{ "CostCenter"="00123" }).ResourceGroupName

删除标记

若要删除特定标记,请使用 Update-AzTag 并设置为 -OperationDelete. 传递要删除的标记的资源 ID。

$removeTags = @{"Project"="ECommerce"; "Team"="Web"}
$resource = Get-AzResource -Name demostorage -ResourceGroup demoGroup
Update-AzTag -ResourceId $resource.id -Tag $removeTags -Operation Delete

将删除指定的标记。

Properties :
        Name        Value
        ==========  =====
        CostCenter  00123

若要删除所有标记,请使用 Remove-AzTag 命令。

$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Remove-AzTag -ResourceId "/subscriptions/$subscription"

后续步骤