KRM
KRM

Reputation: 129

How to bulk update a particular Tag in Azure on the basis of Subscription, Region and another tag?

I need to bulk update a particular tag which is applied on a variety of resources in Azure like VMs, NSGs, AGWs, RGs, so on and so forth. I need to update this tag for all kind of resources on which they are applied. How can I do the same ? I need to filter resources on basis of Subscription, Region and another tag value ? Can I use AZ CLI or Azure Powershell and if yes, how ? How can I put filter of Subscription, Region and another tag value for bulk tag update ?

Upvotes: 1

Views: 3361

Answers (2)

Joy Wang
Joy Wang

Reputation: 42133

To filter with subscription, you need to use Set-AzContext to set the specific subscription, because azure powershell can just run against one subscription, to see the subscriptions that your logged account can access, you could use Get-AzSubscription.

Then you can filter with region and another tag value, after getting them, update their tags via Update-AzTag.

Connect-AzAccount
Set-AzContext -Subscription "<subscription-id>"
$resources = Get-AzResource -TagName "tag1" -TagValue "value1" | Where-Object {$_.Location -eq 'centralus'}
$Tags = @{"tag1"="value1"; "tag2"="value2";}
$resources | ForEach-Object {
    Update-AzTag -ResourceId $_.ResourceId -Tag $Tags -Operation Merge
}

enter image description here

Upvotes: 2

nobodyfromhell
nobodyfromhell

Reputation: 349

You can use Azure PowerShell:

$mergedTags = @{"key1"="value1"; "key3"="value3";}
Update-AzTag -ResourceId /subscriptions/{subId} -Tag $mergedTags -Operation Merge

Original documentation: https://learn.microsoft.com/en-us/powershell/module/az.resources/update-aztag?view=azps-5.3.0

or Az Cli: https://learn.microsoft.com/en-us/cli/azure/tag?view=azure-cli-latest

Upvotes: 0

Related Questions