Query a List of Servers from Active Directory

I needed a list of all servers listed in AD. There are many scripts out there that do this, but they didn’t quite fit my needs, so I put together one from a few different scripts.

strADDomainName = "domainname.com"

strBase = ";"
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objFileOut = objFSO.OpenTextFile ("listofservers.txt", 2, True)

strFilter = "(&(objectclass=computer)(objectcategory=computer)(operatingSystem=*Server*)(cn=*));"
strAttrs = "cn;"
strScope = "subtree"
Set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"

Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
objRS.MoveFirst
While Not objRS.EOF
	WScript.Echo objRS.Fields(0).Value
	objFileOut.WriteLine(LCase(objRS.Fields(0).Value) & ", " & LCase(objRS.Fields(0).Value) & "." & strADDomainName)
	objRS.MoveNext
WEnd

objFileOut.Close
Set objFileOut = Nothing
Set objFSO = Nothing

Reference: http://www.intellipool.se/forum/index.php?showtopic=849

Leave a Reply