Return Host names, and corresponding virtual machine(s) for Hyper-V Cluster

by May 3, 2017

Hi All,

I have a Hyper-V Cluster consisting of several Hyper-V Host Computers. I want to enumerate the list of Hosts with PowerShell and return two columns of data featuring the Host name, and Virtual Machine(s) names, like so:

Host | Virtual Machine
VH01 | VM01
VH01 | VM02
VH01 | VM03
VH02 | VM01
VH02 | VM02
…. | …..

I have the code below which can give me a complete list of VMs, however it shows this information without reference to the Hosts.

Powershell
<# Retrieve the Virtual Hosts for the current Hyper-V Cluster #>
$EduVirHosts = Get-ClusterNode

foreach($VirHost in $EduVirHosts)
{
$VirtMachs = Get-VM -ComputerName $VirHost
$VirtMachs

}

Can anybody offer any coding advice here? I thought initially that a Hash Table may be the way to go, but then it occurred to me that keys need to be unique, and in this instance they are not.

Any help here would be greatly appreciated.

Kind Regards,

Davo