Searching Active Directory with ADSI and VBScript

When I’m looking for something in the Active Directory I don’t want to spend 30 seconds clicking [+] boxes to expand trees and manually look for a user or computer object so I created this script to do the job of finding it for me and telling me exactly where I have to go to find it.

'==========================================================================
'
' NAME: search_ad.vbs
'
' AUTHOR: Lewis Roberts
'
' COMMENT:   Returns the full canonical name of an LDAP source if found in
'      the Active Directory. You can search for anything that would
'      be found in Active Directory.
'
'==========================================================================
 
On Error Resume Next
 
ADSRoot = "DC=yourdomain,DC=co,DC=uk"
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
objCommand.CommandText = _
"SELECT * FROM 'LDAP://" & ADSRoot & "' WHERE objectCategory='user' OR objectCategory='computer'"
 
srchTrm = LCase(InputBox("Please enter your search term.", "Active Directory Search"))
i = 0
 
If Len(srchTrm) < 4 or Instr(srchTrm, "=") Then
  WScript.Echo "Please enter a search term in excess of 3 characters. Don't use ""="""
  WScript.Quit
End If
 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
  strADPath = LCase(objRecordSet.Fields("adsPath").Value)
  
  If InStr(strADPath, srchTrm) Then
    Set objUser = GetObject(objRecordSet.Fields("adsPath").Value)
        
    Select Case objUser.Class
      Case "user"
        strMsg = "Type: Person" & VbCrLf
        strMsg = strMsg & "Name: " & objUser.DisplayName & VbCrLf
        strMsg = strMsg & "Email: " & objUser.mail & VbCrLf
        strMsg = strMsg & "Telephone: " & objUser.telephoneNumber & VbCrLf
        strMsg = strMsg & "Department: " & objUser.department & VbCrLf
        strMsg = strMsg & "Title: " & objUser.title & VbCrLf
      Case "computer"
        strMsg = "Type: Computer" & VbCrLf
        strMsg = strMsg & "Name: " & objUser.Get("name") & VbCrLf
        strMsg = strMsg & "OS: " & objUser.operatingSystem & VbCrLf
      Case Else
        strMsg = "Unidentified" & VbCrLf
    End Select
    'strMsg = strMsg & "LDAP: " & objUser.adsPath & VbCrLf
    objUser.GetInfoEx Array("canonicalName"), 0
    strMsg = strMsg & "AD Path: " & objUser.canonicalName
    WScript.Echo strMsg & VbCrLf 
    i = i + 1
  End If
  objRecordSet.MoveNext
Loop
 
If Not i > 0 Then
  WScript.Echo "No results found."
End If

2 Responses to “Searching Active Directory with ADSI and VBScript”

  1. Gary says:

    Rather than explicitly referencing your domain root, you can use the following:

    Set objRootDSE = GetObject(“LDAP://RootDSE”)
    ADSRoot = objRootDSE.Get(“rootDomainNamingContext”)

  2. Lewis says:

    Thanks Gary, I remember writing this as I was still exploring VBScript and you’re absolutely right, that would make the script instantly portable to other domains.

Leave a Reply