Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tags are merged when adding a new one with Azure CLI #5

Open
FBoucher opened this issue Sep 6, 2019 · 2 comments
Open

Tags are merged when adding a new one with Azure CLI #5

FBoucher opened this issue Sep 6, 2019 · 2 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@FBoucher
Copy link
Owner

FBoucher commented Sep 6, 2019

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 FBoucher added help wanted Extra attention is needed bug Something isn't working labels Sep 6, 2019
@adeelaleem
Copy link

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
}
}
}

@FBoucher
Copy link
Owner Author

FBoucher commented Oct 1, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
Development

No branches or pull requests

2 participants