Hello all,
Is there a way to query W2K Active Directory for a telephone number and
return the user's e-mail address in VBA? I generated a list of calls
daily into an Excel spreadsheet and I would like to send the user an
automated e-mail from that spreadsheet.
Any help will be appericated,
Thanks
Matt
Yes.
I ran:
cscript //nologo c:\temp\temp.vbs "1 (770)475-3820"
and received:
1 (770)475-3820 (e-mail address removed)
c:\temp\temp.vbs contains:
On Error Resume Next
Dim objConnection, objCommand, objRootDSE, strDNSDomain
Dim strFilter, strQuery, objRecordSet, oArgs
Set oArgs = WScript.Arguments
Phone = oArgs(0)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(telephoneNumber=" & Phone & "))"
strAttributes = "mail"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 1
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
mail = objRecordSet.Fields("mail")
Wscript.Echo Phone & " " & mail
objRecordSet.MoveNext
Loop
objConnection.Close
Set objConnection = Nothing
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing
See the example at tip 9843 » How can I use VBScript to return all the users in my domain?
in the 'Tips & Tricks' at
http://www.jsifaq.com
Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com