I have an Active Directory with several different OUs containing user
accounts. I am trying to write a script to get the email address of any
user. I would like the only info going into the script to be the
username, because it will not know what OU any given user is in.



All the binding examples I have seen require you to specify the OU or
group name. How can I bind to an object if I don?t know where it is?


--
Posted via http://dbforums.com

Using ADSI to get info from AD by Tintin

Tintin
Wed Oct 22 17:18:31 CDT 2003

Hi,

you can start your query on top of the AD. (subtree).
Here "DC=YourDom,DC=LOCAL"

Keep in mind that a user may have more than one e-mail
address, and that in this case the information is stored
in an array. See code below. Don't be confused, for some
resaon I have had to drop the leading "smtp:".

Notice: LDAP-Search is limited to 1000 hits, by default.











-----
output1 = "out1.txt"
strSearchList = "sAMAccountName, Displayname,
mailNickname,proxyAddresses"

proxyZeile = ""


Set dObjekt = CreateObject("Scripting.FileSystemObject")
Set File1 = dObjekt.OpenTextFile(output1,2,true,0)

Set ADOconn = CreateObject("ADODB.Connection")
ADOconn.Provider = "ADSDSOObject"
ADOconn.Open "ADs Provider"

'Execute Query, if succesfull -> RecordSet
'bstrADOQueryString = "<LDAP://DC=YourDom,DC=LOCAL>;(&
(objectCategory=Person)(objectClass=user));" &
strSearchList & ";subtree"
bstrADOQueryString = "SELECT ADsPath, samAccountname
FROM 'LDAP://DC=YourDom,DC=LOCAL' WHERE objectCategory
= 'Person' AND objectClass = 'User'"

Set objRS = ADOconn.Execute(bstrADOQueryString)

'Read RecordSet
On Error Resume Next

While Not objRS.EOF

proxy = objRS.Fields("proxyaddresses")

If IsArray(proxy) Then
smtp = ""
proxyZeile = ""
For i = LBound(proxy) To UBound(proxy)

If proxy(i) <> "" Then
proxyZeile = proxyZeile & proxy(i) & "%"

' If Lcase(Left(proxy(i),4)) = "smtp" Then
' Wscript.Echo proxy(i)
' smtp = smtp & proxy(i) & "%"
' End If

End If

Next

Wscript.echo "smtp : " & smtp
Wscript.echo "Length : " & Len(smtp)
Wscript.echo "Length -1: " & Len(smtp) -1
Wscript.echo "smtp -1: " & Left(smtp,Len(smtp) -1)
File1.Writeline objRS.Fields("sAMAccountName") & ";" &
objRS.Fields("Displayname") & ";" & Left(smtp,Len(smtp) -1)

Else
Wscript.Echo "Typ unknown !!"
smtp = ""

End If

File1.Writeline objRS.Fields("sAMAccountName") & ";" &
objRS.Fields("Displayname") & ";" & Left(smtp,Len(smtp) -1)

objRS.MoveNext

If Err then
wscript.Echo err.number & ":" & Err.Description
wscript.quit
end if

Wend

'Close Connections
objRS.Close
ADOconn.Close
Set objRS = Nothing
Set ADOconn = Nothing
File1.Close