VBScript: Use an LDAP Query to Find All Windows Servers on a Domain

Damn, the ADsDSOObject rocks! This script, which weighs in at less than 20 lines, finds all machines running any form of Windows Server on a given domain. Note that this script isn’t useful in finding domain controllers, but rather any machine running Windows Server.

‘****************************************************************************
‘ This script created by Chrissy LeMaire ([email protected])
‘ Website: http://netnerds.net/

‘ This script finds all machines running Windows Server (NT, 2000, 2003) in AD

‘Msgbox output provides server name and OS version.

‘ NO WARRANTIES, USE THIS AT YOUR OWN RISK, etc.
‘*****************************************************************************

Set objAdRootDSE = GetObject(“LDAP://RootDSE”)
Set objRS = CreateObject(“adodb.recordset”)

varConfigNC = objAdRootDSE.Get(“defaultNamingContext”)
strConnstring = “Provider=ADsDSOObject”
strWQL = “SELECT * FROM ‘LDAP://” & varConfigNC & “‘ WHERE objectCategory= ‘Computer’ and OperatingSystem = ‘Windows*Server*'”
objRS.Open strWQL, strConnstring
Do until objRS.eof
Set objServer = GetObject(objRS.Fields.Item(0))
strServerName = objServer.CN
strOperatingSystem = objServer.OperatingSystem
MsgBox strServerName & ” is running ” & strOperatingSystem
objRS.movenext
Set objServer = Nothing
Loop
objRS.close

Set objRS = Nothing
Set objAdRootDSE = Nothing

Also, I found this nice reference of Command One Liners while searching the web. Totally handy!

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in Active Directory, Networking, VBScript