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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LDAP Search of AD

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
Got some good code from a Tek-tips member to get a list of all members of an OU in AD. I want to tailor that to get a list of all users with a certain email address and all users with a certain name. However, no matter what syntax I use, the recordset returned is always empty. This is one variation:

Code:
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "<LDAP://OU=myOU,DC=myCompany,DC=local>;(email=' & emailAddr & "');name;onelevel"
Set rs = cmd.Execute

For the actual command text I have tried other variations:

Code:
cmd.CommandText = "<LDAP://OU=myOU,DC=myCompany,DC=local>;(targetAddress=' & emailAddr & "');name;onelevel"
cmd.CommandText = "<LDAP://OU=myOU,DC=myCompany,DC=local>;(mail=' & emailAddr & "');name;onelevel"
cmd.CommandText = "<LDAP://OU=myOU,DC=myCompany,DC=local>;(name=' & contactName & "');name;onelevel"
cmd.CommandText = "<LDAP://OU=myOU,DC=myCompany,DC=local>;(cn=' & contactName & "');name;onelevel"

Can anyone tell me the proper syntax to check for a certain email address or to check for a certain name.

I could just get every record in the particular OU and then loop through them looking for a match. But I am adding several hundred contacts to AD and I would have to parse through the full list every time I add one.


Mighty
 
Its ages since i did any scripting for AD but I remember the email address one being strange.

If I remember correctly there is any LDAP viewer you can instal on your machine that has a drop down menu for all te possible LDAP options.

Check again against your emailaddr
 
Figured it out - not supposed to use the quotes apparently:

Code:
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = "<LDAP://OU=myOU,DC=myCompany,DC=local>;(email=" & emailAddr & ");name;onelevel"
Set rs = cmd.Execute

Mighty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top