Adding Proxy Addresses in Exchange 2003 by script

I need to add additional proxy-addresses to a bunch ofl users via a script.
The user list is available in a CSV file with the following format:


No 'samAccount' or 'ADSpath' attributes there.

How do you find the ADSpath of the user, given only his primary SMTP e-mail address?  You need the ADSpath in order to append the new secondary smtp address, don't you?

Would appreciate if anyone can assist me with the script.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:


Just the usual "in case you're not familiar with" bit: This is VbScript, it will need saving as .vbs for it to work.

It will go through AD checking the Mail attribute of each user account. If everything is working correctly this should reflect the Primary SMTP Address of the account. If not let me know and I'll modify it so it checks all proxy addresses.

It won't make any changes at the moment, both the lines that actually make the change are commented out:

                        ' objUser.Put "proxyAddresses", arrAddresses
                        ' objUser.SetInfo

Remove the ' from those lines when you're happy with what it's up to.

All changes (or lack of) are written out to a log file called logfile.txt which will appear in the same folder as the script itself. I've written it to cope with input exactly as you've written above, if that's even slightly different then the Delimiter Constant is likely to need rewriting.

Otherwise, I hope this is helpful.


Option Explicit

Const CSV_FILE = "file.txt"

Dim objFileSystem, objFile, objStream, objUsers, objConnection, objCommand, objRootDSE, objRecordSet, objUser
Dim strPrimarySMTP, strNewSMTP, strAddresses
Dim arrLine, arrAddresses

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.GetFile(CSV_FILE)
Set objStream = objFile.OpenAsTextStream(1, 0)

Set objUsers = CreateObject("Scripting.Dictionary")

Do While Not objStream.AtEndOfStream
      arrLine = Split(objStream.ReadLine, FILE_DELIMITER)

      If UBound(arrLine) = 1 Then
            strPrimarySMTP = LCase(arrLine(0))
            strNewSMTP = LCase(arrLine(1))

            If Not objUsers.Exists(strPrimarySMTP) Then
                  objUsers.Add strPrimarySMTP, strNewSMTP
            End If
      End If

Set objStream = Nothing
Set objFile = Nothing

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 aDSPath, mail " &_
      "FROM 'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' WHERE objectClass='user'"
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 objFile = objFileSystem.OpenTextFile("logfile.txt", 2, True, 0)

While Not objRecordSet.EOF
      If Not IsNull(objRecordSet.Fields("mail")) Then
            strPrimarySMTP = LCase(objRecordSet.Fields("mail"))

            If objUsers.Exists(strPrimarySMTP) Then
                  Set objUser = GetObject(objRecordSet.Fields("aDSPath"))

                  arrAddresses = objUser.Get("proxyAddresses")

                  ' Check the Address isn't already there

                  strAddresses = Join(arrAddresses, ",")
                  If InStr(1, strAddresses, ",smtp:" & objUsers(strPrimarySMTP) & ",", VbTextCompare) = 0 Then
                        ReDim Preserve arrAddresses(UBound(arrAddresses) + 1)
                        arrAddresses(UBound(arrAddresses)) = "smtp:" & objUsers(strPrimarySMTP)

                        objFile.WriteLine strPrimarySMTP & ": Matched to " & objUser.Get("name") &_
                              ": " & objUsers(strPrimarySMTP) & " Address Added"

                        ' objUser.Put "proxyAddresses", arrAddresses
                        ' objUser.SetInfo
                        objFile.WriteLine strPrimarySMTP & ": Matched to " & objUser.Get("name") &_
                              ": " & objUsers(strPrimarySMTP) & " Address Exists"
                  End If
                  Set objUser = Nothing
            End If
      End If


Set objFile = Nothing
Set objFileSystem = Nothing

Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing

Set objUsers = Nothing

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
juraganAuthor Commented:
Hi Chris,

Thanks for the script. I just came back from a trip, so I'll let you know soon how it is going :)

- J -
juraganAuthor Commented:
The script works great!  Thanks!
Get Blueprints for Increased Customer Retention

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Chris DentPowerShell DeveloperCommented:

You're welcome. Have a good christmas :)

juraganAuthor Commented:
Merry Christmas! :)
If I I wanted to run this against an OU, I can replace the LDAP://RootDSE with LDAP://ou=Finance,dc=fabrikam,dc=com, right?
Chris DentPowerShell DeveloperCommented:

Not quite, no.

You would need to modify these lines:

Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT aDSPath, mail " &_
      "FROM 'LDAP://" & objRootDSE.Get("defaultNamingContext") & "' WHERE objectClass='user'"
Set objRootDSE = Nothing

All RootDSE does for us is find the "DC=fabrikam,DC=com" portion of the domain without us having to write it. In your case you want to change the CommandText to this:

objCommand.CommandText = "SELECT aDSPath, mail " &_
      "FROM 'LDAP://ou=Finance,dc=fabrikam,dc=com' WHERE objectClass='user'"

That is used as the base for the search, the connection point. Because we set ADS_SCOPE_SUBTREE it will go from that point to any child OU.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.