1

I am looking for a way to create one tag and use it to tag multiple resources. These resources I am tagging are not all in the same resource group / subscription and they are all different types of resources ranging from VMs to App services to Log Analytics Workspaces etc. There are over 3000 resources in total. The ultimate goal is to create a tag for each resource type, and then use powershell to filter by resource type and then send a particular command to each resource type. Below is an example.

VM Resources will get VM-Cleanup-Tag, and then I will run a command that will add that tag to all VM resources that I specify. Then I will run a command to filter by that tag and pipe another command in there such as stop-AzureVM. And then I will do that same thing for many other resource types. Any ideas will be greatly appreciated!

1 Answers1

1

Just use the generic AzResource commands for this purpose

$tags = @{"VM-Cleanup-Tag"="True" ; "ResoucreType" = "VM" ; "etc"="etc"}
Get-AzResource -ResourceType Microsoft.Compute/virtualMachines | Set-AzResource -Tag $tags -Force

Then you can call of of those resources

$VMs_Cleanup = Get-AzResource -Tag @{"VM-Cleanup-Tag" = "True"}
$VMs_Cleanup | Stop-AzVM

or

foreach ($VM in $VMs_Cleanup) {
  Stop-AzVM $VM
  blah blah
}

Get Tags and append new ones

$tags = @{"VM-Cleanup-Tag"="True" ; "ResoucreType" = "VM" ; "etc"="etc"}
$Resource = Get-AzResource -Name $VMName
$tags.Keys | % {$Resource.Tags.Add($_,$tags.Item($_))}
Set-AzResource $VMName
Bevan
  • 1,305
  • 1
  • 11
  • 17
  • Thanks but the real question is how can I mass-apply the tag to the resources if they are in different subscriptions / tenants. There can be hundreds of subscriptions, and we need to make sure we are only targeting specific resources in specific subscriptions, and not just the resource type across all subscriptions / tenants. There is also the concern that by applying the tag to all those resources that they will lose any tags they currently have assigned to them, can you clarify if that is actually the case? @Bevan – Kyle Monteagudo Aug 23 '19 at 21:48
  • There is definitely enough info in the example I've given and can easily be scaled according to your requirements in this comment. You'll need to have your tenants, subscriptions, resource names each in their own array then loop through them accordingly in nested loops. Then my example would simply be the execution part of your script. You may end up using different parameters such as `Name` instead of/along side `ResourceType`. You'll need to test the behaviour on tags. You can get existing tags with the same `Get` command then add to the returned array. What have you tried so far? – Bevan Aug 24 '19 at 02:28
  • Bevan, thanks for that information. From what I see here it looks like I should be able to do this. I do still have the question about whether adding the task using PS will remove the existing tags that are on the resources as when I looked at Microsoft Docs it says that it will in fact remove any existing tags when running the command to add the tag. Do you know a way to make it so that those existing tags aren't removed or overwritten? – Kyle Monteagudo Aug 24 '19 at 04:17
  • Just updated answer. Please note that I haven't gone through this process before so test it all out thoroughly. Should work though. – Bevan Aug 26 '19 at 11:55
  • trying for the same scenario with AzureCLI and AzureDevops pipeline, Any guidance please. https://stackoverflow.com/questions/74722439/azure-resource-tag-bulk-update-using-azuredevops-pipeline – Vowneee Dec 07 '22 at 20:38