PowerShell: Obtain List of Hyper-V Hosts in All Domains of All Forests

# This function scans the environment of forests and returns a list of Hyper V Hosts within all domains of those forests. Note that the result shall include the Windows OS version as Hyper-V Managers (HM) are OS dependent (e.g. 2012 HM maynot be able to control 2008 hosts).

# Ensure that AD management module is available for PS Session
if (!(get-module -name "ActiveDirectory") ){
Add-WindowsFeature RSAT-AD-PowerShell | out-null;
import-module -name "ActiveDirectory" -DisableNameChecking | out-null;
}

function List-hyperVHosts {
[cmdletbinding()]
param(
[string]$forest
)
try {
Import-Module ActiveDirectory -ErrorAction Stop
} catch {
Write-Warning "Failed to import Active Directory module. Cannot continue. Aborting..."
break;
}

$domains=(Get-ADForest -Identity $forest).Domains
foreach ($domain in $domains){
#"$domain`: `n"
[string]$dc=(get-addomaincontroller -DomainName $domain -Discover -NextClosestSite).HostName
try {
$hyperVs = Get-ADObject -Server $dc -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"' -ErrorAction Stop;
} catch {
"Failed to query $dc of $domain";
}
foreach($hyperV in $hyperVs) {
$x = $hyperV.DistinguishedName.split(",")
$HypervDN = $x[1..$x.Count] -join ","

if ( !($HypervDN -match "CN=LostAndFound")) {
$Comp = Get-ADComputer -Id $HypervDN -Prop *
$OutputObj = New-Object PSObject -Prop (
@{
HyperVName = $Comp.Name
OSVersion = $($comp.operatingSystem)
})
$OutputObj
}
}
}
}

function listForests{
$GLOBAL:forests=Get-ADForest | select Name;
if ($forests.length -gt 1){
#for ($i=0;$i -lt $forests.length;$i++){$forests[$i].Name;}
$forests | %{$_.Name;}
}else{
$forests.Name;
}
}

function listHyperVHostsInForests{
listForests;
$forests | %{List-HyperVHosts $_.Name;}
}

listHyperVHostsInForests;

Sample Output:

PS C:\WINDOWS\system32> List-HyperVHosts

KIMCONNECT.COM:
OSVersion HyperVName
--------- ----------
Windows Server 2016 Standard HYPERV01
Windows Server 2008 R2 Enterprise HYPERV02
Hyper-V Server HYPERV03
Hyper-V Server 2012 R2 HYPERV04
Windows Server 2012 R2 Standard HYPERV05
Windows Server 2012 R2 Standard HYPERV06

Leave a Reply

Your email address will not be published. Required fields are marked *