troubleshooting Question

Need Script that pulls Primary SMTP Address from specific OU

Avatar of KristenHoward
KristenHoward asked on
VB Script
13 Comments1 Solution1591 ViewsLast Modified:
I found this script, and it does pull just the Primary SMTP addresses, which is what I want, but it pulls from the entire Active Directory. I tried to manipulate the script to pull from a specific OU (after LDAP://), but am unsuccessful. Would like some help with this script, or if someone else has another one that will export to a .csv what I need, that would be great. Here is the script:

Const FILE_NAME = "Addresses.csv"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT name, mail " &_
        "FROM 'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' WHERE " &_
        "objectClass='user' AND objectCategory='person' AND mail='*'"
Set objRootDSE = Nothing
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 600
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile(FILE_NAME, 2, True, 0)
objFile.WriteLine "Name,SMTP Email"
Do While Not objRecordSet.EOF
        strName = objRecordSet.Fields("name").Value
        strMail = objRecordSet.Fields("mail").Value
        objFile.WriteLine strName & "," & strMail
Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objFile = Nothing
Set objFileSystem = Nothing
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros