Link to home
Start Free TrialLog in
Avatar of philltx
philltx

asked on

vbscript ldap : How do I ldap query with an email address using vbscript?

I'm trying to write a vbscript which executes and pulls the Office field for a user in active directory.  I only have their smtp email address.  Is there a way to do this?  I can do it by cn however I haven't been able to query by email?

If necessary, would I have to go through MS Exchange to do this and then use the displayname to query active directory?  

I know i can do it this way: 
Set MyUser = GetObject ("LDAP://cn=" & uname & ",ou=" & strname & ",DC=bobdom,DC=net")
 
I tried it this way but it failed: 
Set MyUser = GetObject ("LDAP://mail=" & uname & ",ou=" & strname & ",DC=bobdom,DC=net")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of philltx
philltx

ASKER

thanks for the snippet.

When I changed the strEmail to an email address which is listed in active directory, I didn't receive any output or errors.  Would having different ou's in AD affect the way the code queries AD?

thanks
Oops - yes, let's set it to query all paths...



Const ADS_SCOPE_SUBTREE = 2
 
strEmail = "someone@somewhere.com"
 
Dim objRoot : Set objRoot = GetObject("LDAP://RootDSE")
Dim objDomain : Set objDomain = GetObject("LDAP://" & objRoot.Get("defaultNamingContext"))
Dim cn : Set cn = CreateObject("ADODB.Connection")
Dim cmd : Set cmd = CreateObject("ADODB.Command")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
Set cmd.ActiveConnection = cn
 
cmd.CommandText = "SELECT ADsPath FROM '" & objDomain.ADsPath & "' WHERE email='" & strEmail & "'"
cmd.Properties("Page Size") = 1000
cmd.Properties("Timeout") = 300
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
Dim objRS : Set objRS = cmd.Execute
Do While Not objRS.EOF
  wscript.echo objRS.Fields(0)
Loop
 
Set objRS = Nothing
Set cmd = Nothing
Set cn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing

Open in new window

Avatar of philltx

ASKER

thanks for the quick response,

I'm still getting the same result.. no errors or anykind of output.
Is there a way to diagnose why there is no output?

bah - my mistake...change
 WHERE email=
to
 WHERE mail=

(just remove the 'e')
Avatar of philltx

ASKER

i removed the "e" and still the same result... would assigning it a domain help?

thanks
assigning it a domain?
Should be in the format of user@domain.com, no?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of philltx

ASKER

Hey it works! however it seems to loop indefinetly.  How do i get this to stop?
Avatar of philltx

ASKER



Also, I am having trouble setting the function name getOffice = objRS.Fields(0).  I get a type mismatch error.  Any ideas on this?

Hi, not sure if you're still needing this, but try changing this:

Do While Not objRS.EOF
  wscript.echo objRS.Fields(0)
  intRecords = intRecords + 1
  objRS.MoveNext
Loop

to this
While Not objRS.EOF
  wscript.echo objRS.Fields(0)
  intRecords = intRecords + 1
  objRS.MoveNext
Wend


and also, is getOffice a function you've defined?  Perhaps you need to typecast the return value:
This would return a string:
getOffice = CStr(objRS.Fields(0).Value)
This would return an integer:
getOffice = CInt(objRS.Fields(0).Value)

Regards,

Rob.
Const ADS_SCOPE_SUBTREE = 2
 
strEmail = "Fname.Lname@yourcompany.com"
 
Dim objRoot : Set objRoot = GetObject("LDAP://RootDSE")
Dim strDNSDomain : strDNSDomain = "LDAP://" & objRoot.Get("defaultNamingContext")
Dim cn : Set cn = CreateObject("ADODB.Connection")
Dim cmd : Set cmd = CreateObject("ADODB.Command")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
WScript.Echo strEmail

Set cmd.ActiveConnection = cn
 
cmd.CommandText = "SELECT ADsPath FROM '" & strDNSDomain & "' WHERE mail='" & strEmail & "'"
cmd.Properties("Page Size") = 1000
cmd.Properties("Timeout") = 300
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
Dim objRS : Set objRS = cmd.Execute
intRecords = 0
While Not objRS.EOF
  wscript.echo objRS.Fields(0)
  intRecords = intRecords + 1
  objRS.MoveNext
Wend
 
WScript.Echo intRecords & " records were returned."
Set objRS = Nothing
Set cmd = Nothing
Set cn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing