Solved

VB Script to add an Email address to an existing user

Posted on 2009-03-31
20
708 Views
Last Modified: 2012-05-06

Hi All,

I am looking for a script which will add a Email Address to an existing user object. The email address should be in the form of firstname.lastname@domain.com.

We are running on Exchange 2003.
0
Comment
Question by:madhusk
  • 11
  • 9
20 Comments
 
LVL 27

Expert Comment

by:bluntTony
ID: 24027489
Is there a reason for not using a recipient update polciy in Exchange System Manager?
0
 
LVL 5

Author Comment

by:madhusk
ID: 24027755
I mean the recipient policy(firstname.lastname) is there. I just want to add an email address to the user in the same format as recipient policy so that there are no further issues.

Can you please post the script
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24027773
OK, the vbscript to add an address would be:
See this link: http://www.microsoft.com/technet/scriptcenter/resources/qanda/may05/hey0510.mspx
Although the recipient policy should be applying the correct email address to the users if working correctly.

Const ADS_PROPERTY_APPEND = 3

Set objUser = GetObject("LDAP://<DN of user>") 

objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", Array("email address")

Open in new window

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24027810
Right, I undertand - you want to add another email address to the user that isn't specified in the user policy. Gotcha. The above script should do this.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24027885
One other thing I forgot (sorry!) - the set info method. The complete code below.
You need to enter the address as 'smtp:person@company.com'
That should do it.

Const ADS_PROPERTY_APPEND = 3

Set objUser = GetObject("LDAP://<DN of user>") 

objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", Array("email address")

objUser.SetInfo

Open in new window

0
 
LVL 5

Author Comment

by:madhusk
ID: 24028267
Tony,

Thanks for the help, but can you modify the script such that it reads the user names(logon ID) from a text file and add the Email address to the user objects.

Thanks,
Madhu
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24028315
Should be possible. Give me a while and I'll post a script....
0
 
LVL 5

Author Comment

by:madhusk
ID: 24028490
Thank you
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24028724
When you say logon ID, I assume you mean sAMAccountName (i.e. the 'user' bit in domain\user) - is this correct?
Also, how do we know what the email address to add should be - what format should it be? e.g. lastname.firstname@company.com?
I'll need to know this before I can finish the script...
0
 
LVL 5

Author Comment

by:madhusk
ID: 24029510
you are correct,

it is the samaccount name and the email address should be firstname.lastname@company.com
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24031838
The below script searches your current domain for user objects. It checks each user object - if it's sAMAccount name matches one in the text file list you have provided, it forms the email address firstname.lastname@company.com and adds it to the user.
The search isn't case sensitive, and it echoes a message each time it finds a user and adds an email address. If you want to avoid the windows pop up dialogs, run via cscript, e.g.
cscript update_emails.vbs
You just need to change the two variables strEmailSuffix and strTextFilePath to suit your needs.
Hope this is OK. I've tested myself but would recommend you do so also.

Const ForReading = 1

Const ADS_PROPERTY_APPEND = 3
 

'CHANGE THESE TWO VARIABLES

'---------------------------------------------------------------

strEmailSuffix = "@company.com"

strTextFilePath = "D:\testfile.txt"

'---------------------------------------------------------------
 

Set oRootDSE = GetObject("LDAP://RootDSE")

Set fso = CreateObject("Scripting.FileSystemObject")

Set objTxt = fso.OpenTextFile(strTextFilePath,ForReading)

Set objDict = CreateObject("scripting.dictionary")
 

strBase   =  "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;"

strFilter = "(&(objectclass=user)(objectcategory=person));" 

strAttrs  = "distinguishedName;"

strScope  = "subtree"
 

While Not objTxt.AtEndOfStream

	strEntry = UCase(objTxt.ReadLine)

	objDict.Add strEntry, strEntry

Wend
 

Set objConn = CreateObject("ADODB.Connection")

objConn.Provider = "ADsDSOObject"

objConn.Open "Active Directory Provider"

Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)

objRS.MoveFirst

while Not objRS.EOF

    Set objUser = GetObject("LDAP://" & objRS.Fields(0).Value)'Connect to user.

		If objDict.Exists(UCase(objUser.sAMAccountName)) Then

			strNewEmail = "smtp:" & objUser.givenName & "." & objUser.sn & strEmailSuffix

			WScript.Echo "User: " & objUser.sAMAccountName & ", adding email address : " & strNewEmail

			objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", Array(strNewEmail)

			objUser.SetInfo

		End If    		

    objRS.MoveNext

Wend
 

Set fso = Nothing

Set objTxt = Nothing

Set objDict = Nothing

Set oRootDSE = Nothing

Open in new window

0
 
LVL 5

Author Comment

by:madhusk
ID: 24048011
Hi when I executed the file I was getting the attached error
error.jpg
0
 
LVL 27

Accepted Solution

by:
bluntTony earned 250 total points
ID: 24048199
Strange - it's works fine over here. I've added in some more output to try and figure out what the problem is. It looks like the initial query for the user's DN is returning null.
Run the below code. It should give you a count of the user objects found, then it'll show which user it's trying to connect to each time. Let me know the output...

Const ForReading = 1

Const ADS_PROPERTY_APPEND = 3

Const verbose = True
 

'CHANGE THESE TWO VARIABLES

'---------------------------------------------------------------

strEmailSuffix = "@nsa.com"

strTextFilePath = "D:\testfile.txt"

'---------------------------------------------------------------
 

Set oRootDSE = GetObject("LDAP://RootDSE")

Set fso = CreateObject("Scripting.FileSystemObject")

Set objTxt = fso.OpenTextFile(strTextFilePath,ForReading)

Set objDict = CreateObject("scripting.dictionary")
 

strBase   =  "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;"

strFilter = "(&(objectclass=user)(objectcategory=person));" 

strAttrs  = "distinguishedName;"

strScope  = "subtree"
 

While Not objTxt.AtEndOfStream

	strEntry = UCase(objTxt.ReadLine)

	objDict.Add strEntry, strEntry

Wend
 

Set objConn = CreateObject("ADODB.Connection")

objConn.Provider = "ADsDSOObject"

objConn.Open "Active Directory Provider"

Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)

If verbose Then WScript.Echo "Total User objects found in " & strBase & " : " & objRS.RecordCount
 

objRS.MoveFirst

While Not objRS.EOF

	If verbose Then WScript.Echo "Attempting to connect to : " & objRS.Fields(0).Value

    Set objUser = GetObject("LDAP://" & objRS.Fields(0).Value)'Connect to user.

		If objDict.Exists(UCase(objUser.sAMAccountName)) Then

			strNewEmail = "smtp:" & objUser.givenName & "." & objUser.sn & strEmailSuffix

			WScript.Echo "User: " & objUser.sAMAccountName & ", adding email address : " & strNewEmail

			objUser.PutEx ADS_PROPERTY_APPEND, "proxyAddresses", Array(strNewEmail)

			objUser.SetInfo

		End If    		

    objRS.MoveNext

Wend
 

Set fso = Nothing

Set objTxt = Nothing

Set objDict = Nothing

Set oRootDSE = Nothing

Open in new window

0
 
LVL 5

Author Comment

by:madhusk
ID: 24048914
Now it says 1000 users found and it says connecting to users one by one.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24048944
OK, has it added the email addresses to the users as expected? Did you specify the required account names in the text file? The account name should be in the list as:
UserName
...as opposed to :
DOMAIN\UserName
 
0
 
LVL 5

Author Comment

by:madhusk
ID: 24049088
I have specified correctly only user name rather than domain\username.

But I do not want the script to access each and every account and try to change the properties. I want the script to only work on the user names which i have provided in the text file.
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24049204
The script needds to connect to each user object in order to be able to check the sAMAccountName. That's what the output is showing - it's not making chages to them all. I just made this output more verbose to try and troubleshoot your problem earlier, but it looks like that has gone away now.
You can switch off the verbose output by changing the 3rd line of the script to :
Const verbose = False
It builds a list of account names from the file, then connects to each user, and check the user's sAMAccountName against the list. If there's a match, it'll add the email address.
Test it on one account - just add one account name to the text list and run the script. When the script hits this user, it'll advise you that it's added the email address. Then go and check in ADUC that the email address is list on the 'Email Addresses' tab of the user.
0
 
LVL 5

Author Comment

by:madhusk
ID: 24051040
Ok Great, let me give it a try
0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24086840
Any joy with this?
0
 
LVL 5

Author Comment

by:madhusk
ID: 24609951
yes it worked great, sorry for delay in closing the question
0

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now