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

Filed under: Active Directory, Networking, Quick Code, VBScript — Written by Chrissy on Tuesday, January 9th, 2007 @ 9:02 pm

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.

Awesome

'****************************************************************************
' This script created by Chrissy LeMaire (clemaire@gmail.com)
' 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!

3 Comments   -
  • Comment by Jeffery Hicks | January 11, 2007 @ 7:09 am

    I'm glad you found the one-liners handy. If you come up with any good ones, I hope you'll share.

  • Comment by Mike | February 13, 2007 @ 1:58 pm

    Thank you!

    This is just what I have been looking for!

    Mike

  • Comment by MHC | June 17, 2008 @ 7:47 am

    Perfect, works right out of the box.

Leave your comment