Wednesday, September 17, 2014

Assigning Tags with PowerCLI


A friend of mine had a challenge, identify which VM belongs to which owner !!!
The reason for that is simple. Since here is a service provider ,being able to identify it’s customer’s VMs help them to better manage the environments and specific customer’s needs.

Someone could argue why not to use folders ?!? well, folders would help you to identify just one aspect of  the VM, what if you need to identify every web server belongs to client A ? well why not create subfolders ?  Do you see where I’m going to ? handling hundreads of folders is not ideally.

vCenter 5.1 brough the ability to Tag it’s inventory, meaning you can tag one or more vCenter objects with one or more tags. Then you could easily search them using those tags as well.

Without further ado, let’s see how we accomplished that:

- Go to vCenter, click at HOME, Tags and then at New Category
that will identify the purpose of our tags


- Give it a Name and a Description.
Since it’s intented just for VMs I’m selecting just it. (you could make your tag available for many objects types).


- Click on New Tag to create the tag itself

 - Give it a Name, a Description and pick up the Category we just created before.
Create a tag for each Customer you want to identify


 Now the funny part.
Since you might have hundreds of VMs to Tag, tagging them manually is a hell of a job, so why not use a script for that ?!?

I’m using PowerCLI to manage this task, if you need some information to get it started check the PowerCLI basics here.

- Create a CSV file including the Name of the VM and the Tag you want to assign to them.
You can export the VMs from vCenter running: Get-VM | select Name | Export-CSV c:\temp\vms.csv –NoTypeInformation

Be aware of the first line, which identifies the headers, you will need them later.


- Copy the CSV to a location where you can access (I used c:\temp)

Now let’s Tag those VMs

- On PowerCLI run: Import-CSV c:\temp\vms.csv | % {New-TagAssignment -Tag  $_.Customer -Entity $_.Entity}

It’s done.

To see which VM has been tagged run: Get-TagAssignment


You can also filter based on a specific Customer: Get-VM -Tag “Customer” | select Name


Be aware I’m not the best script guy out there, there might be smatter ways of doing that, use your imagination ; )
The script is provided as is, without any guarantee, make sure you run it on a non-productive environment first to make sure it’s safe.

To learn more about PowerCLI Tag snap-in, click here.

No comments:

Post a Comment

Who am I

My photo
I’m an IT specialist with over 15 years of experience, working from IT infrastructure to management products, troubleshooting and project management skills from medium to large environments. Nowadays I'm working for VMware as a Consulting Architect, helping customers to embrace the Cloud Era and make them successfully on their journey. Despite the fact I'm a VMware employee these postings reflect my own opinion and do not represents VMware's position, strategies or opinions.

Most Viewed Posts

Blog Archive