PowerShell: How to return all the VMs in a Hyper-V Cluster
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
.
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.