Tags - jasper-zanjani/azure GitHub Wiki

Notes

:point_right: Use tags to organize your Azure resources and management hierarchy

Tags are case-insensitive key-value pairs used to organize Azure resources.

Tasks

The Az.Resources module offers the new commands New-AzTag and Update-AzTag since version 1.12.0. :point_right: Use tags to organize your Azure resources and management hierarchy

List tags

$r = Get-AzResource -Name r -ResourceGroup rg
Get-AzTag -ResourceId $r.id

Resource Group

$rg = Get-AzResourceGroup -Name rg
Get-AzTag -ResourceId $rg.ResourceId

Subscription

$s = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Get-AzTag -ResourceId "/subscriptions/$s"
az resource show -n r -g rg --resource-type "Microsoft.Network/virtualNetworks" --query tags

Resource group

az group show -n rg --query tags

List by tag

(Get-AzResource -Tag @{ CostCode="1001"}).Name

Providing only tag name, with no value

(Get-AzResource -TagName CostCode).Name

Resource group

(Get-AzResourceGroup -Tag @{ CostCode="1001" }).ResourceGroupName
az resource list --tag Dept=Finance

Resource group

az group list --tag CostCode=1001

Apply tags to resource, overwriting

$tags = @{"Dept"="Finance"; "Status"="Normal"}
New-AzTag -ResourceId $resource.id -Tag $tags
Set-AzResource -ResourceId $r.ResourceId -Tag @{ CostCode="1001"; Environment="Production" } -Force

Resource group

Set-AzResourceGroup -Name rg -Tag @{CostCode=1001; Environment=Production}
az resource tag --tags 'Dept=IT' 'Environment=Test' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"

Resource group

az group update -n examplegroup --tags 'Environment=Test' 'Dept=IT'
az group update -n rg --set tags.Environment=Production tags.CostCode=1001

Apply tags to resource without overwriting

$r = Get-AzResource -ResourceName hrvm1 -ResourceGroupName rg
$r.Tags.Add("Owner", "[email protected]")
Set-AzResource -Tag $r.Tags -ResourceId $r.ResourceId -Force

Resource group

$tags = @{"Dept"="Finance"; "Status"="Normal"}
$rg = Get-AzResourceGroup -Name demoGroup
New-AzTag -ResourceId $rg.ResourceId -tag $tags
$tags = (Get-AzResourceGroup -Name rg).Tags
$tags.Add("Owner", "[email protected]")
Set-AzResourceGroup -Tag $tags -Name rg
jsonrtag=$(az group show -n rg --query tags)
rt=$(echo $jsonrtag | tr -d '"{},' | sed 's/: /=/g')
az group update -n rg --tags $rt [email protected]

Remove tags

Specific tags

$tags = @{"Project"="ECommerce"; "Team"="Web"}
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Delete

Remove all tags

$s = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
Remove-AzTag -ResourceId "/subscriptions/$s"
Set-AzResourceGroup -Tag @{} -Name rg