Hyper-V: How to Find Virtualization Hosts (Servers with Hyper-V Enabled)

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 you can use how 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
Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Danny van Dam edited Revision 11. Comment: added note about that this might not apply to Hyper-V 3.0

  • FZB edited Revision 4. Comment: typo

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • FZB edited Revision 4. Comment: typo

  • nice

  • awesome!

  • This is really nice. Tell me, what does one need to do to have this kind of prowess in scripting?

  • Danny van Dam edited Revision 11. Comment: added note about that this might not apply to Hyper-V 3.0

Page 1 of 1 (5 items)