使用 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 會取代資源、資源群組或訂用帳戶上的所有標記。 呼叫命令後,會傳入您要標記的實體資源識別碼。

下列範例會將一組標記套用至儲存體帳戶:

$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 標記從「一般」變更為「綠色」

$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 命令,並傳入實體的資源識別碼。

若要查看資源的標記,請使用:

$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 並將 -Operation 設定為 Delete。 傳入您要刪除標籤的資源識別碼。

$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"

下一步