POSTS
VMware PowerCLI and Get-View filters
I have been confused by how to best use the -filter parameter on Get-View so I decided to dig around a little. The examples that are given always seem to be (semi)exact matches and I wanted to do an OR in my filter. Per the help this did not appear possible, but after some digging I found that it is possible for a single property at least.
The help has this to say about the Filter parameter:
Specify a hash of - pairs, where represents the property value to test, and represents a pattern the property must match. If more than one pair is present, all the patterns must match.
Ok. So it can be a pattern; what exactly does that mean? I dug through the code using Reflector and found that the comparison is being done with a regular expression (RE). This was great news for me. That means the following should work and it does.
Get-View -ViewType VirtualMachine -Filter @{"Name"="^vm001$|^vm003$"}
This example is a little silly, but you can use this technique to build up some pretty specific filters and hopefully help your code to run faster. Here is an example to determine the VMs running on two hosts.
Get-View -ViewType VirtualMachine -Filter @{"Runtime.Host"="^host-86561$|^host-86566$"}
Be careful with extra matching when using REs. You will definitely want the start/end anchors if you want to match a specific property exactly, otherwise you will get extra matches which could be VERY ugly. The match method is being called using the IgnoreCase option so case sensitivity does not matter.
As with other REs you can do lots of things. You can check out MSDN or just do a Get-Help about_Regular_Expressions to get an idea of what is possible. The Regulator is a great resource for testing and exploring .NET regular expressions. Please be sure to test that the expression works the way you want. It can be easy to make a mistake and include or exclude items that you did not intend to.
Check out the documentation for the method FindEntityView() to get some more ideas of the matches possible. It is referenced on page 13 of this document:Developer’s Guide: VMware Infrastructure .NET Toolkit 1.0.