Find all Hyper-V hosts and windows virtual machines in your AD environment

Several Microsoft products use Service Connection Points (SCP) to advertise themselves using objects stored in Active Directory. Clients query the directory to locate services. Microsoft Hyper-V, as I mentioned, also registers or publishes its information in AD as a service connection point. Now, we can use ADSI or AD cmdlets or Quest AD cmdlets to retrieve this information.

To retrieve a list of all “Hyper-V hosts” in the AD environment,

Using ADSI

$domainName = "YourDomainNameHere"
$DSearcher= New-Object directoryServices.DirectorySearcher($domainName)
$DSearcher.filter="(&(CN=Microsoft Hyper-V)(objectCategory=serviceConnectionPoint))"
$hostDN = $DSearcher.Findall()
$hostDN | ForEach-Object {
   $_.path.split(",")[1].replace("CN=","")
}

Using Microsoft AD cmdlets

Import-Module ActiveDirectory
$hostDN = Get-ADObject -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Microsoft Hyper-V"'
$hostDN | ForEach-Object { $_.DistinguishedName.Split(",")[1].replace("CN","") }

Using Quest AD cmdlets

#Open Quest ActiveRoles Management Shell
Get-QADObject -Name  'Microsoft Hyper-V' -Type  serviceConnectionPoint | Get-QADComputer -Identity  {$_.ParentContainerDN}

The above methods retrieve the name of the computers running Hyper-V role.

To retrieve a list of all “Windows Virtual Machines” in the AD environment,

Using ADSI

$domainName = "YourDomainNameHere"
$DSearcher= New-Object directoryServices.DirectorySearcher($domainName)
$DSearcher.filter="(&(CN=Windows Virtual Machine)(objectCategory=serviceConnectionPoint))"
$hostDN = $DSearcher.Findall()
$hostDN | ForEach-Object {
   $_.path.split(",")[1].replace("CN=","")
}

Using Microsoft AD cmdlets

Import-Module ActiveDirectory
$hostDN = Get-ADObject -Filter 'ObjectClass -eq "serviceConnectionPoint" -and Name -eq "Windows Virtual Machine"'
$hostDN | ForEach-Object { $_.DistinguishedName.Split(",")[1].replace("CN","") }

Using Quest AD cmdlets

#Open Quest ActiveRoles Management Shell
Get-QADObject -Name  'Windows Virtual Machine' -Type  serviceConnectionPoint | Get-QADComputer -Identity  {$_.ParentContainerDN}

This is it! If you observe the above code, I just changed the Name property alone to either “Microsoft Hyper-V” or “Windows Virtual Machine”. I tested the above commands only on Windows Server 2008 R2 and Windows Virtual machines only. I am not sure if Linux or other virtual machines running on Hyper-V get registered or not. I don’t have or need any of them in my setup ;-)