Apply Tags to Azure Resources based on Resource Group tags via PowerShell

Hi everyone, its been a while since my last post and the main reason is that I now work on Azure with customers these days. Boooo I hear all the ConfigMgr fans say. I know, I know I'm still a fan of CM myself.

Today’s blog is sharing with you a PowerShell script that I wrote for one of my customers that apply’s Tags to all the Resources in a Resource Group, based on the Tags’s applied to that Resource Group.

The scenario here is that in this particular case billing was being charged by the Tag’s applied to each resource. The issue my customer had was

1)Tags for Resources are not inherited by default from their Resource Group

2)Some of their processes at this point in time meant there was no way for them to ensure the correct Tag’s were applied to each resource.

3)They needed to ensure that if somebody has applied a Tag not for billing that it doesn’t disappear. As an example Environment : Development.

So lets run through this scenario below

I have created two Resource Groups with Tags which we can see below

image

In AzureDogs I have one Managed Disk with no Tags

image

In ConfigMgrDogs I have two Managed Disks one with a single Tag  Environment : Production, the other with no Tags

image

The script should run through each resource and save the non billing tags and apply the Resource Group tags.

Here is a link to the script on GitHub. Feel free to clone this and make any improvements. This can easily be tweaked and uploaded to Azure Automation and run on a nightly schedule.

Once the script is run we can see in my output that it has applied the appropriate Tags to each Resource

image

When we look back in the portal we can see that our resources now have the billing tags and the OSDisk3 resource has also kept its original custom environment Tag.

image

image

I hope this helps you out.