Fun with vSphere Tags

By | April 8, 2015

I originally was going to put up a script for creating tag categories and tags and assigning them, but of course Alan Renouf had a script on his site!

The problem I found is that if an object needs to change a tag (say with a cardinality of single), you can just do a set-tagassignment and overwrite the existing tag.

You must REMOVE the tag and then assign the updated tag like so:


Note, serverList has objects that have properties ServerName and Status (along with other properties I have left off of this example)


My goal was to use tags as a way to double check that I was protecting all of the correct VMs in SRM (by comparing various tags that matched my criteria).

The next problem I found though was that when you search tags, if you provide a list of tags they will be implicitly or’d ie match any tag in the list. I have different categories, with some categories having possibly values that would work for me. I needed a way to do match ‘ANY’ and then find what VMs are a member of all of the lists (the intersection of all of the lists).

Thanks to googling, I found how to do an intersection in Powershell. The snippet below will get three groups of tags that represent three of my categories. The last line does an intersection by filtering each array successively. The end result is the $resultsVMs variable only have to be members of each category (logical AND)

2 thoughts on “Fun with vSphere Tags

  1. Graham D

    Awesome post solved a problem for me. I now know how to select my VMs by multiple tags by both OR or AND matches

    Absolutely brilliant. This content should be in VMWare’s docs


Leave a Reply

Your email address will not be published. Required fields are marked *