I recently had a need to locate a specific VM in a cluster. Turns out, there’s no truly direct way to do it. Therefore, I have developed a simple script that will allow you to find one, many, or all VMs in a cluster.

This script isn’t entirely necessary. You can fairly easily retrieve all these VMs with the following:

That works, and works pretty well. But then, I came up with another need: I only wanted to look at highly available VMs. That changes things. You could do this:

There’s nothing wrong with this, but it’s a bit much for regular use. Of course, the-powers-that-be in the PowerShell world would likely tell me to just stick that in a .ps1 and make sure it’s accessible. But it’s still less flexible than I would like. So, I started working on something more elegant. The more I worked on it, the more complicated it got. The end result is actually a one-liner, although I broke it out into multiple lines because legibility trumps cleverness in a saved script. The best part of this is that it’s pretty simple to use. You can just run Get-ClusterVM without any parameters from a cluster node and it will retrieve all the highly available VMs on the entire cluster. You can use the –VMName parameter to select one or more specific VMs. You can also use –Cluster to remotely target a cluster. Another nice thing about this is, in my unscientific testing, it seems to be a little quicker than the Get-VM run. I’m assuming that’s because once it gets to the point of querying for VMs, it queries for them with very specific parameters instead of using open-ended wildcards. There is a potential shorter path that would work for most situations. You could just query cluster groups, as the name is usually matched to the VM. However, it is entirely possible to have a VM name that is different from its cluster resource name, especially if you’re using VMM. The only guaranteed option they match on is the VmID. The complete script is below. It works on 2012 and above. Just save it as Get-ClusterVM.ps1 and execute from PowerShell. Use Get-Help to view the parameters.

[optin-monster-shortcode id=”lwzgfpb294pntqna”]