Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ldap query sub OU's

Status
Not open for further replies.

cdtech9198

IS-IT--Management
Feb 14, 2008
48
US
Hello. First of all I want to say thank you for all the great help. This site is a great resource.

q: I am running a LDAP query using a text stream. The users.txt stream consist of sAMAccountnames.

firstname.lastname
etc
etc

The scripts runs fine when I target a specific OU however I am looking to target sub OU's for these users (or even better the entire AD).


I am not sure what I am doing wrong. When I target a specific OU and a user is not found the script just continues, but when I target a seperate OU with no users name it errors:The error I receive is
"Provider: One or more errors occurred during processing of command."

The script is a continuation of one provided by Marcdmac

Code:
Const ADS_PROPERTY_APPEND = 3
Const ADS_PROPERTY_DELETE = 4
Const ForReading = 1
Dim qQuery, objConnection, objCommand, objRecordSet, obj
Dim oRootDSE, strDomain, fso, ts, strOU, strOU2, strData

Set oRootDSE = GetObject("LDAP://rootDSE")
strDomain = oRootDSE.get("defaultNamingContext")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("users.txt", ForReading)


Do until ts.AtEndofStream



'WScript.Echo strData

strOU = "testou"
'strOU2 = "testsubou"

strData = ts.Readline

qQuery = "SELECT distinguishedName FROM 'LDAP://'OU="& strOU & "," & strDomain & "' WHERE objectCategory='user' " & _
        " AND sAMAccountName =  '"&strData&"' " 
'checks if ldap is formatted 
'WScript.Echo qQuery


Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = qQuery
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
    Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName"))
    arrEmails = objUser.ProxyAddresses
    
For Each email In arrEmails
        If Left(email,4)="SMTP" Then
        'Test to echo default email address
        WScript.Echo  email
                   End If
    Next
   
        objRecordSet.MoveNext
Wend

objConnection.Close

Loop
 
silly me. I add a ' in the wrong spot "LDAP://'OU".
wrong
Code:
qQuery = "SELECT distinguishedName FROM 'LDAP://'OU="& strOU & "," & strDomain & "' WHERE objectCategory='user' " & _
        " AND sAMAccountName =  '"&strData&"' "

corrected
Code:
qQuery = "SELECT distinguishedName FROM 'LDAP://OU="& strOU & "," & strDomain & "' WHERE objectCategory='user' " & _
        " AND sAMAccountName =  '"&strData&"' "

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top