How to Add New EMail Address and Set it as Default in Exchange 2003 using vbScript

Without boring you with the whole story why I had to write this script, it is just because I don't want to open each user account in Active Directory and add new email addresses manually.
I just can't stand that kind of a repetitive job.  With this, I can modify a bulk of users from a list.
The script is well commented so that it'd be easy to understand what each line will do.

'==========================================================================
'
' NAME: AddNewDefaultAddress.vbs
'
' AUTHOR: Tito Castillote , june.castillote@gmail.com, shaking-off-the-cobwebs.blogspot.com
' DATE  : 11/9/2012
'
'
' Usage instructions:
' 1. You must create a text file (Userlist.txt) containing the list of user DN (distinguished names) and the new email address that you want to add and set as default.
'  ex. CN=Administrator,CN=Users,DC=LabWorks,DC=local SMTP:Administrator@email.spam
'    - the DN and Email address must be seperated with a TAB. This script is designed for TAB-delimited Input file.
'
' 2. This script and the Input file must be in the same location and then run this from the Command Prompt.
'  cscript //nologo AddNewDefaultAddress.vbs
'
' COMMENT: If you plan to modify a bulk of users, I'd suggest that you do it in batches and not all at once.
'
'========================================================================

'Declare Constants to avoid "Invalid Procedure Calls or Arguments" errors.
Const ADS_PROPERTY_CLEAR = 1, ADS_PROPERTY_UPDATE = 2, ADS_PROPERTY_APPEND = 3
Const ForReading = 1, ForWriting = 2, ForAppending = 8

'Create File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Open the Input file
Set MbxName = objFSO.OpenTextFile("UserList.txt", ForReading) 

'Read the inout file from beginning to end
Do Until MbxName.AtEndofStream 

'Read the current line
nUser=MbxName.ReadLine

'Split the current line into an array with TAB as delimiter
userArray=Split(nUser, vbTab) 

'Get the Object with DN name stored in UserArray(0)
Set objUser = GetObject ("LDAP://" & UserArray(0))

'Get information about the Object (User) and start modifying values.
objUser.GetInfo 

'Assign the ProxyAddress (all email address of the current user) to variable strProxyAddress
strProxyAddress = objUser.GetEx("ProxyAddresses")

'Clear the ProxyAddress of the current user
objUser.PutEx ADS_PROPERTY_CLEAR, "proxyaddresses", 0

'Disable the "Automatically update the e-Mail addresses based on Policy"
objUser.PutEx ADS_PROPERTY_UPDATE, "msExchPoliciesExcluded", Array("{26491CFC-9E50-4857-861B-0CB8DF22B5D7}")

'Save
objUser.SetInfo

'This is just to display which user is being processed.
WScript.Echo "Processing User: " & objUser.DisplayName 

'Loop through the list of email addresses stored in variable strProxyAddress and assign to variable strAddress
For Each strAddress In strProxyAddress 

If strAddress<>UserArray(1) Then ' <--- This is a test to make sure that you are not adding an already existing email address.
'Append the current email address stored in variable strAddress and make sure that it is not set as Default. = Array(replace(strAddress,UCase("SMTP:"),LCase("smtp:"))
objUser.PutEx ADS_PROPERTY_APPEND, "proxyaddresses", Array(replace(strAddress,UCase("SMTP:"),LCase("smtp:")))
'Save
objUser.SetInfo 
End If      
Next

'Append the new email address from the Input File stored in UserArray(1) variable.
objUser.PutEx ADS_PROPERTY_APPEND, "proxyaddresses", Array(UserArray(1))
'Save
objUser.SetInfo
Loop

'Close the Input file
MbxName.Close

'Clean up
Set objUser = Nothing
Set strProxyAddress = Nothing
Set MbxName = Nothing
Set nUser = Nothing
Set UserArray = Nothing



Share:

Popular Posts

Powered by Blogger.