Mailbox Cleanup After Cross Organizational Moves

I thought I would share some code I wrote for doing bulk mailbox cleanups on Exchange 2003 for cross-organizational mailbox moves. This code is nearly identical to that in my previous post, but this one takes an input file to process several mailboxes at once. The input file must be tab-separated and contain the distinguished name in the first column and the smtp email address in the second. No headers are necessary on the input file.


'*** Declare Variables


Dim oFS '*** Object variable for file system object

DIM strUserDN '*** This is the distinguished name of the user (string)

DIM objUser '*** Object variable for containing the user

DIM strEmailAddr '*** This becomes the external-to-exchange email address that mail is forwarded to

DIM sFile '*** String variable that holds the file name to open from the parameters

DIM oArgs '*** Array Variable to hold parameter objects

DIM iFile '*** Object variable that links to the file object being opened

DIM Values '*** Holds the CSV values in an Array


'*** Initialize and Declare Variables and Parameters


On Error Resume Next


'*** Grab the file name from the arguments command line

Set oArgs = wscript.arguments

sFile = oArgs(0)


'*** Open the file, check the file, make sure it is valid format

Set oFS = CreateObject("Scripting.FileSystemObject")

Set iFile = oFS.OpenTextFile(sFile, 1)


'*** Loop through the file, and reset all mailbox properties


Do Until iFile.AtEndofStream

Do '*** Inner Do Loop


       Values = Split(iFile.Readline, Chr(9))


       '*** Set variables

       strUserDN = Values(0)

       strEmailAddr = Values(1)


       '*** Connect to the user and disable mailbox

       wscript.echo "Connecting to user " & strUserDN

       Set objUser = GetObject("LDAP://" & strUserDN)


       If Err<>0 Then

              wScript.Echo "[ERROR] - COULD NOT BIND TO OBJECT " & strUserDN & ". sKIPPING USER"

              wScript.Echo ".... " & Err.Description

              Exit Do

       End If


       wscript.echo ".... Removing mailbox"




       If Err<> 0 Then


              wScript.Echo ".... " & Err.Description

              wScript.echo ".... Original Mailbox should be intact for user."

              Exit Do

       End If


       wscript.echo ".... setting email address: " & strEmailAddr

       objUser.MailEnable strEmailAddr

       objUser.Put "internetEncoding",1310720



       If Err<> 0 Then

              wScript.Echo "[ERROR] - COULD NOT MAIL-ENABLE USER."

              wScript.Echo ".... " & Err.description

              wScript.Echo "Mailbox has been disabled, but user is not in a mail-enabled state."

       End If


       Exit Do

Loop '*** Inner Do Loop