Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: How to return all the VMs in a Hyper-V Cluster

I'm a first time programmer with PowerShell. Running on Windows Server 2012.

I'm trying to get a list of all VM's on my failover cluster and am working with this:

$clusterNodes = Get-ClusterNode | select Name 
ForEach($item in $clusterNodes)
{Get-VM -ComputerName $item}

And this returns a bunch of errors

However, this works perfectly fine

$hosts = "server1", "server2", "server3", "server4"
ForEach($item in $hosts)
{Get-VM -ComputerName $item}

Is it failing because Get-ClusterNode | select Name returns the following?

Name
----
server1
server2
server3
server4

with a heading and underline?

like image 498
Paul S. Avatar asked Jan 28 '14 15:01

Paul S.


1 Answers

These one liners maybe a little easier. Works for Windows Server 2012 R2, should work for 2012.

Get-VM –ComputerName (Get-ClusterNode –Cluster CLUSTER)

Basically gets the nodes from the cluster called "CLUSTER". Feeds list to your -ComputerName

OR

Get-ClusterGroup -Cluster CLUSTER | ? {$_.GroupType –eq 'VirtualMachine' } | Get-VM

Gets the cluster groups and filters for type called "VirtualMachine".

With either, you can execute Get-ClusterGroup instead of Get-ClusterGroup -Cluster CLUSTER if you are on one of the nodes.

like image 170
ScraperDave Avatar answered Sep 21 '22 18:09

ScraperDave