共用方式為


使用 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綠色

$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

當你將參數設 -OperationReplace時,新的標籤集合會取代現有的標籤。

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

後續步驟