AzSubscriptionCleaner icon indicating copy to clipboard operation
AzSubscriptionCleaner copied to clipboard

Tags are merged when adding a new one with Azure CLI

Open FBoucher opened this issue 4 years ago • 2 comments

Let's say I already have 2 tags:

  • owner= frank
  • demo=meetup

When running the code:

jsonrtag=$(az resource show -g summerDemo -n demoWebsite --resource-type "Microsoft.Web/sites" --query tags)

rt=$(echo $jsonrtag | tr -d '"{},\n' | sed 's/: /=/g')

az resource tag --tags $rt expireOn=2019-08-29 -g summerDemo -n demoWebsite --resource-type "Microsoft.Web/sites"

The final result will be 2 tags like this:

  • expireOn=2019-08-29
  • owner= frank demo=meetup

FBoucher avatar Sep 06 '19 15:09 FBoucher

We can also use below script to add tag on each resources from resource group: $group = Get-AzResourceGroup "RGBP-Pipeline" if ($null -ne $group.Tags) { $resources = Get-AzResource -ResourceGroupName $group.ResourceGroupName foreach ($r in $resources) { $resourcetags = (Get-AzResource -ResourceId $r.ResourceId).Tags if ($resourcetags) { foreach ($key in $group.Tags.Keys) { if (-not($resourcetags.ContainsKey($key))) { $resourcetags.Add($key, $group.Tags[$key]) } } Set-AzResource -Tag $resourcetags -ResourceId $r.ResourceId -Force } else { Set-AzResource -Tag $group.Tags -ResourceId $r.ResourceId -Force } } }

adeelaleem avatar Sep 12 '19 22:09 adeelaleem

Thanks @adeelaleem, excellent idea it's been added to the PowerShell example. in the main ReadMe.md

FBoucher avatar Oct 01 '19 00:10 FBoucher