Home > Active Directory, ADSI, LDAP, Microsoft, Scripting, VBScript, WMI > VBScript: Query multiple servers on any domain from AD or a text file.

VBScript: Query multiple servers on any domain from AD or a text file.

I had a requirement to query AD for all servers then gather their network config.  I then found that there were servers in AD that had been shut down but not cleaned up, so the script failed on the first unreachable server, so I had to output a list from AD to a text file then re-work the script to read from that text file.  In this article I’ll show both methods, the worker code and how I output the list of servers.

Enumerate all servers in the domain and query their NIC settings:

Option Explicit

Dim objRootDSE, strDNSDomain, adoConnection, adoCommand, strQuery
Dim adoRecordset, strComputerName, strBase, strFilter, strAttributes

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory for all computers.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on computer objects with server operating system.
strFilter = "(&(objectCategory=computer)(operatingSystem=*server*))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "cn"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

' Enumerate computer objects with server operating systems.
Do Until adoRecordset.EOF

    ' Worker Code: Enumerate computer objects with server operating systems
    strComputerName = adoRecordset.Fields("cn").Value
    Wscript.Echo "================================="
    Wscript.Echo "Computer Name: " & strComputerName
    Wscript.Echo "================================="
    ' Bind to CIMV2 on each Server
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")
    ' Gather Network Adapter Configuration
    Set colAdapters = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
    n = 1
    WScript.Echo
    For Each objAdapter in colAdapters
        WScript.Echo
        ' Display NIC Name
        WScript.Echo "Network Adapter " & n
        WScript.Echo "================="
        WScript.Echo "  Description: " & objAdapter.Description
        ' Display MAC address
        WScript.Echo "  Physical (MAC) address: " & objAdapter.MACAddress
        ' Display IP address
        If Not IsNull(objAdapter.IPAddress) Then
            For i = 0 To UBound(objAdapter.IPAddress)
                WScript.Echo "  IP address:             " & objAdapter.IPAddress(i)
            Next
        End If
        ' Display Subnet Mask
        If Not IsNull(objAdapter.IPSubnet) Then
            For i = 0 To UBound(objAdapter.IPSubnet)
                WScript.Echo "  Subnet:                 " & objAdapter.IPSubnet(i)
            Next
        End If
        ' Display Default Gateway
        If Not IsNull(objAdapter.DefaultIPGateway) Then
            For i = 0 To UBound(objAdapter.DefaultIPGateway)
                WScript.Echo "  Default gateway:        " & _
            objAdapter.DefaultIPGateway(i)
        Next
        End If
        ' Display DNS Server Search Order
        WScript.Echo
        WScript.Echo "  DNS"
        WScript.Echo "  ---"
        WScript.Echo "    DNS servers in search order:"
        If Not IsNull(objAdapter.DNSServerSearchOrder) Then
            For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
                WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
            Next
        End If
        WScript.Echo "    DNS domain: " & objAdapter.DNSDomain
        ' Display Suffix Search List
        If Not IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
            For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)
                WScript.Echo "    DNS suffix search list: " & _
                objAdapter.DNSDomainSuffixSearchOrder(i)
            Next
        End If
        n = n + 1
    Next
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
Function WMIDateStringToDate(utcDate)
WMIDateStringToDate = CDate(Mid(utcDate, 5, 2)  & "/" & _
Mid(utcDate, 7, 2)  & "/" & _
Left(utcDate, 4)    & " " & _
Mid (utcDate, 9, 2) & ":" & _
Mid(utcDate, 11, 2) & ":" & _
Mid(utcDate, 13, 2))
End Function

Query AD for a list of servers by saving the following script as QueryADServers.vbs:

Option Explicit

Dim objRootDSE, strDNSDomain, adoConnection, adoCommand, strQuery
Dim adoRecordset, strComputerName, strBase, strFilter, strAttributes

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory for all computers.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on computer objects with server operating system.
strFilter = "(&(objectCategory=computer)(operatingSystem=*server*))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "cn"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

' Enumerate computer objects with server operating systems.

Do Until adoRecordset.EOF

    ' Worker Code: Enumerate computer objects with server operating systems & display their names
    strComputerName = adoRecordset.Fields("cn").Value
        Wscript.Echo "Computer Name: " & strComputerName
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
Function WMIDateStringToDate(utcDate)
WMIDateStringToDate = CDate(Mid(utcDate, 5, 2)  & "/" & _
  Mid(utcDate, 7, 2)  & "/" & _
    Left(utcDate, 4)    & " " & _
      Mid (utcDate, 9, 2) & ":" & _
        Mid(utcDate, 11, 2) & ":" & _
          Mid(utcDate, 13, 2))
End Function

Output a list of servers to a text file by running the script thusly:

C:\>cscript //nologo QueryADServers.vbs > ServerList.txt

NB: Once I output the list of servers I fed the list into a batch file that ran an nslookup against them and removed the ones that didn’t have an entry in DNS.  The resulting ServerList.txt file is the one I used for the following script.

Query the ServerList.txt text file by running the following script from the same directory that the ServerList.txt file is in as Administrator:

Option Explicit

Dim objFSO, objFile, strLine, objWMIService, colAdapters, n, objAdapter, i

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("ServerList.txt", 1)

Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine

    ' Worker Code: Enumerate computer objects with server operating systems
    strComputerName = adoRecordset.Fields("cn").Value
    Wscript.Echo "================================="
    Wscript.Echo "Computer Name: " & strLine
    Wscript.Echo "================================="
    ' Bind to CIMV2 on each Server
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")
    ' Gather Network Adapter Configuration
    Set colAdapters = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
    n = 1
    WScript.Echo
    For Each objAdapter in colAdapters
        WScript.Echo
        ' Display NIC Name
        WScript.Echo "Network Adapter " & n
        WScript.Echo "================="
        WScript.Echo "  Description: " & objAdapter.Description
        ' Display MAC address
        WScript.Echo "  Physical (MAC) address: " & objAdapter.MACAddress
        ' Display IP address
        If Not IsNull(objAdapter.IPAddress) Then
            For i = 0 To UBound(objAdapter.IPAddress)
                WScript.Echo "  IP address:             " & objAdapter.IPAddress(i)
            Next
        End If
        ' Display Subnet Mask
        If Not IsNull(objAdapter.IPSubnet) Then
            For i = 0 To UBound(objAdapter.IPSubnet)
                WScript.Echo "  Subnet:                 " & objAdapter.IPSubnet(i)
            Next
        End If
        ' Display Default Gateway
        If Not IsNull(objAdapter.DefaultIPGateway) Then
            For i = 0 To UBound(objAdapter.DefaultIPGateway)
                WScript.Echo "  Default gateway:        " & _
            objAdapter.DefaultIPGateway(i)
        Next
        End If
        ' Display DNS Server Search Order
        WScript.Echo
        WScript.Echo "  DNS"
        WScript.Echo "  ---"
        WScript.Echo "    DNS servers in search order:"
        If Not IsNull(objAdapter.DNSServerSearchOrder) Then
            For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
                WScript.Echo "      " & objAdapter.DNSServerSearchOrder(i)
            Next
        End If
        WScript.Echo "    DNS domain: " & objAdapter.DNSDomain
        ' Display Suffix Search List
        If Not IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
            For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)
                WScript.Echo "    DNS suffix search list: " & _
                objAdapter.DNSDomainSuffixSearchOrder(i)
            Next
        End If
        n = n + 1
    Next
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
Function WMIDateStringToDate(utcDate)
WMIDateStringToDate = CDate(Mid(utcDate, 5, 2)  & "/" & _
Mid(utcDate, 7, 2)  & "/" & _
Left(utcDate, 4)    & " " & _
Mid (utcDate, 9, 2) & ":" & _
Mid(utcDate, 11, 2) & ":" & _
Mid(utcDate, 13, 2))
End Function

If you save this script as QueryServersFromTextFile.vbs you can run it as follows to output the servers NIC configs to a text file:

C:\>cscript //nologo QueryServersFromTextFile.vbs > ServerNICs.txt

Sample output:

=================================
Computer Name: DC1.yourdomain.com
=================================

Network Adapter 1
=================
Description: HP Network Team #1
Physical (MAC) address: 00:11:22:33:44:55
IP address:             192.168.77.100
Subnet:                 255.255.255.0
Default gateway:        192.168.77.1

DNS
---
DNS servers in search order:
    192.168.77.10
    192.168.77.11
DNS domain: yourdomain.com
DNS suffix search list: theirdomain.com
DNS suffix search list: our-domain.com

Once again, thanks to Richard Mueller and the writers of WMI Script-O-Matic & EZ AD Script-O-Matic for enabling me to make my job so much easier.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: