March 4, 2010

Script to set AD targetAddress property to forward Email during a Transition to new Domain

Filed under: General Development,Information Technology — Tags: , , , — Tim Lefler @ 9:07 am

My company was moving from our in house email system to a completely new infrastructure with a new email domain name and a new Active Directory domain.  The plan was to have users configure a new Outlook profile to connect to the new system at a particular time on the migration day.  Because we have little control over when the users will actually make the transition, I want to make sure any new emaill that arrived at the old mailbox would get forwarded to the new mailbox.  This would help cover the straggler users who keep sending email to users who had already migrated.

So this script does exactly that…. it takes a list of distinguished names in an input file, connects to active directory and sets the Active Directory attribute for “targetAddress”.  This will essentially forward the mail to the targetAddress SMTP location.

The input file can be easily generated with a CSVDE command:

C:\csvde -f input.csv -d "dc=domain,dc=com" -r "(ObjectCategory=Person)" -l "DN"
Connecting to "(null)"
Logging in as current user using SSPI
Exporting directory to file input.csv
Searching for entries...
Writing out entries
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
...........................................
Export Completed. Post-processing in progress...
763 entries exported
 
The command has completed successfully

Then you can run this script to read it in and make the changes the the mail enabled active directory objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
' Script to set AD targetAddress attribute on distinguished names from a file created
' via csvde -f input.csv -d "dc=domain.dc=com" -r "(ObjectCategory=Person)" -l "DN"
'
Option Explicit
 
Dim oFSO, sFile, oFile, sText, sDN, strUser, oUser, oLog
Dim sOldDomain, sNewDomain
 
' Change these lines to match your email domains.
sOldDomain="oldemail.com"           
sNewDomain="newemail.com"
 
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "input.csv"
If oFSO.FileExists(sFile) Then
	Set oFile = oFSO.OpenTextFile(sFile, 1)
	set oLog = oFSO.CreateTextFile ("SetTargetAddress.log", True)
	Do While Not oFile.AtEndOfStream
		sText = oFile.ReadLine
     		' Check to make sure we have a line that contains something that looks like a DN
     		If Trim(sText) <> "" and instr(sText,"CN=") Then
			' Strip of the quotes produced by csvde
			sDN = mid(sText,2,len(sText)-2)
			strUser = "LDAP://" & sDN
 
			' Get the User object from Active Directory
                        Set oUser = GetObject(strUser)
 
			if instr(oUser.mail, sOldDomain) then
				oLog.writeline sDN & "," & oUser.mail & "," & oUser.givenName & "." & oUser.sn & "@" & sNewDomain
				' Change the AD attribute
				oUser.targetAddress="SMTP:" & oUser.givenName & "." & oUser.sn & "@" & sNewDomain
                                ' Write the changes back to active directory
                                oUser.SetInfo
			end if
		End If
	Loop
	oFile.Close
	oLog.Close
Else
	WScript.Echo "The file was not there."
End If

and there you have it. Because the targetAddress attribute is set any mail that arrives at the old mailbox is forwarded.

1 Comment »

  1. Nice script and thanks for sharing. I did want to mention that I am surprised you don’t have two users out of 763 that share the same first and last name.

    Also, if anyone tries to run this against a really large domain it would be good to do a little error handling in the script to ensure check for the following:
    1. Your list of users does not include users that share the same first and last name
    2. The targetAddress you are building is actually in use and valid
    3. Log errors if SetInfo fails on a user
    4. Change your LDAP filter to “(&(objectCategory=User)(objectClass=User))” as what you have will return computer objects and contacts. If Exchange is in the domain then change it to “(&(objectCategory=User)(objectClass=User)(mailnickname=*)(homeMDB=*))” to ensure you have users with valid mailboxes.
    5. If oUser.mail is populated, why not just set do this (line 32):
    oUser.targetAddress = “SMTP:” & Replace(oUser.mail,”@oldDomain.com”,”@newdomain.com”)
    That email address can be easily set on the new user account and ensures that it is unique in your domain (assuming new domain is not already populated with some addresses).

    Comment by Brian Kronberg — May 12, 2010 @ 12:45 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress