Hyper-V: How to Find Virtualization Hosts (Servers with Hyper-V Enabled)
Note: This article is based on Hyper-V 2.0 and might not apply to Hyper-V 3.0 (Server 2012)
John Howard, Senior Program Manager in the Hyper-V team at Microsoft, shared a sample VBScript, which you can use to determine which domain-joined servers are running Hyper-V by using the SCP or "Service Connection Point" information in Active Directory.
NOTE: To use the script, edit the call highlighted “DoQuery". Insert the appropriate domain for your organization. Add multiple lines if you have multiple domains. If you want a list of machine names, uncomment the code-block starting "oRecordSet.MoveFirst" and ending "Loop"
Save the contents as "FindServers.vbs" (or download the text file) and run the following from an elevated command prompt:
* "cscript FindServers.vbs"*
On Error Resume Next
Const SCP = "Microsoft Hyper-V"
' Add as many lines as needed for the domains in your org.
DoQuery "DC=YOURDOMAIN,DC=com", "YOURDOMAIN", SCP
Sub DoQuery(szDomainDN, szDomainShortName, szSCP)
Set oConnection = CreateObject("ADODB.Connection")
Set oCommand = CreateObject("ADODB.Command")
oConnection.Provider = ("ADsDSOObject")
oConnection.Open "Ads Provider"
oCommand.ActiveConnection = oConnection
oCommand.Properties("Page Size") = 99
oCommand.Properties("Searchscope") = &H2 'ADS_SCOPE_SUBTREE
oCommand.Properties("Chase Referrals") = &H60
'ADS_CHASE_REFERRALS_ALWAYS
oCommand.CommandText = _
"select distinguishedName from 'LDAP://" & _
szDomainDN & "' " & _
"where objectCategory='serviceConnectionPoint' " & _
"and cn='" & szSCP & "'"
Set oRecordSet = oCommand.Execute
If Err Then
wscript.echo _
"ERROR: Unable to find Domain Rooted at: " & _
szDomainDN
exit sub
End If
If Not oRecordSet.EOF Then
wscript.echo szDomainShortName & ":" & _
oRecordSet.RecordCount
' If you want to enumerate the machine names,
' uncomment this block of code
'oRecordSet.MoveFirst
'Do Until oRecordSet.EOF
' szNodeName = _
' oRecordSet.Fields("distinguishedName")
' 'Trim "CN=<szSCP>,CN="
' szNodeName = _
' mid(szNodeName, InStr(szNodeName,",CN=")+4)
' 'Trim the domain DN
' szNodeName = _
' Left(szNodeName,InStr(szNodeName,",")-1)
' wscript.echo szNodeName
' oRecordSet.MoveNext
'Loop
else
wscript.echo szDomainShortName & ": 0"
end if
set oRecordSet = Nothing
set oCommand = Nothing
oConnection.Close
set oConnection = Nothing
End Sub