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

Paul S. picture Paul S. · Jan 28, 2014 · Viewed 52.1k times · Source

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?

Answer

Trevor Sullivan picture Trevor Sullivan · Jan 28, 2014

Give this a shot:

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

You have to reference the Name property of the objects returned by Get-ClusterNode.