PowerShell: How to return all the VMs in a Hyper-V Cluster
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.
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
.
You can also use Get-ClusterResource since a Cluster Virtual Machine Role is a Cluster Resource.
$clusterResource = Get-ClusterResource -Cluster SomeClusterName | Where ResourceType -eq "Virtual Machine"
Then Get-VM also has a -ClusterObject parameter
Get-VM -ClusterObject $clusterResource
From TechNet -
-ClusterObject Specifies the cluster resource or cluster group of the virtual machine to be retrieved.