Bulk tagging in Azure



We've recently added a whole load of 'on prem' resources to Azure via Azure ARC and we wanted to add some extra tags to those resources to display some more useful information about what they are and who they belong to.

We already had this mapping information in an excel sheet showing that server x is associated with application y and belong to application owner z etc

So all we had to do was take the content of this sheet and somehow apply that as tags to the servers once available in Azure.

To do this again I've just used a simple bit of powershell to read the sheet and then pull out the information into the right format to be assigned to the servers.

This was a little fiddly as the update-aztag cmdlet ( the thing that lets you update tags) expects the tags to be in the format of a hashtable  - this make for a bit of messing around in constructing the input data in the right format.

I managed to run the whole thing in 'cloud shell' (so no messing around with getting a working powershell environment on my local machine) - this can be accessed by the little rectangle with the triangle in the header bar of the portal or directly by going to https://shell.azure.com

Once there you just need to do the following:

1) copy your excel sheet over (you can just drag/drop into the window) my sheet is of the following format:



So I have the following headers : hostname,sub,owneremail,subname,eam,eam,app,lob,desc - i now want to take the data from that and assign that information to the tags on the server mentioned in column A (not the sub/subname things here are not Azure subscriptions - it's for something else :-))

2) Install the additional module for processing excel - this is not a default thing

Install-Module -Name ImportExcel -RequiredVersion 4.0.8

3) Run the following bit of code that will loop through the sheet pulling the details out and applying the tags

import-excel -Path 'tagdata.xlsx'  |ForEach-Object {
write-host ($_.Hostname)
$tags = @{'VPC_SubscriptionId'= "$($_.sub)" ; 'Owner_Email'=  $($_.owneremail) ;  'VPC_SUbscription'=  $($_.subname) ;  'IteraplanID'=  $($_.eam) ;  'VPC_App'= $($_.app) ;  'LOB'=  $($_.LOB) }
$RES = "/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.HybridCompute/machines/"+($_.Hostname)
Update-AzTag -ResourceId $RES -Tag $tags -Operation Merge
$RES = "/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.HybridCompute/machines/"+($_.Hostname)+".domain"
Update-AzTag -ResourceId $RES -Tag $tags -Operation Merge
}


Now due to a quirk in how some of the servers got added into Azure ARC some have domain extension and some don't - the easiest way to deal with that is just to run the tag command twice - then if the domain is not present the first command picks it up, otherwise the second one does - throws a few errors of course but we can just ignore those.

Process took a little while to loop through everything and there did seem to be a very long delay in the updated data being visible in the portal - some sort of caching effect i think. However the get-aztag result confirmed it was set straight away from that point of view.

In our case all of the ARC servers are in the same resource group which then makes the script easy - if you have a more complex setup then the script would need amending slightly.

Same technique can be used for any tagging of course

End result looks something like this:


Happy tagging......


Comments

Post a Comment