How to return all the VMs in a Hyper-V Cluster
Asked Answered
T

7

7

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?

Townswoman answered 28/1, 2014 at 15:7 Comment(0)
M
4

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.

Moldboard answered 28/1, 2014 at 15:9 Comment(0)
A
5

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.

Arsenite answered 9/5, 2016 at 21:0 Comment(0)
M
4

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.

Moldboard answered 28/1, 2014 at 15:9 Comment(0)
P
1

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.

https://technet.microsoft.com/en-us/library/hh848479.aspx

Parmentier answered 18/3, 2015 at 15:49 Comment(0)
G
1

I know this has been answered, but I like this one-liner better:

Get-VM -ClusterObject (Get-ClusterResource | where ResourceType -eq "Virtual Machine")

Or if you're doing it remote, reference the cluster:

Get-VM -ClusterObject (Get-ClusterResource -Cluster name-of-cluster | where ResourceType -eq "Virtual Machine")

The results can be piped into other commands, for example "Set-VMProcessor" or others.

Genteelism answered 15/9, 2016 at 12:49 Comment(0)
R
0

Selecting properties from an object will display the header. You can work around that by piping that list to a loop that outputs just the value:

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

I haven't tested with your code specifically, but I had this same problem last week.

Rameses answered 28/1, 2014 at 15:14 Comment(0)
R
0

I think that the simplest way is:

Get-VM -ComputerName VMCLUSTERNAME

This will return all VMs from your cluster. Sometimes full name with domain is needed.
Everybody forgot that cluster is visible as computer with role Hyper-V in the domain. Also you can access other roles in the cluster if you will think about cluster as normal computer with the roles installed there.
(This is working perfect on powershell in Server 2016)

Rabblerousing answered 4/1, 2018 at 7:25 Comment(0)
G
0

To Get a List of VMs from an SCVMM Cluster we can run the below Script, make sure to modify the cluster name and the VMM server name to match yours:

$Cluster = Get-SCVMHostCluster -Name "hv19cluster" -VMMServer "vmm19n01"
$HostsInCluster = Get-SCVMHost -VMHostCluster $Cluster
#$HostsInCluster | Format-Table -Property Name, VirtualizationPlatform
ForEach ($h in $HostsInCluster) { 

$vm=Get-SCVirtualMachine -VMHost $h 
 
foreach($v in $vm){ write-host ($v)}

}
Guanidine answered 14/2, 2021 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.