Monday, March 26, 2007

Bulk Exmerge

Getting tired of writing scripts at one company, leaving, and having to reinvent the wheel, so I'll store some scripts here. Of course, next year we'll probably be on Exchange 2007 and all this work will be moot (see "Monad"), but just in case. And who knows, maybe somebody else will benefit from this someday, though these are well documented elsewhere (that's how I figured them out after all -- thanks google.)

This script takes an input.txt file full of samAccountNames and creates a Mailboxes.txt file ready for Exmerge to operate on in Bulk mode. Need to then call Exmerge /B /D. The exmerge.ini file needs to be setup with input servername, default destination folder, etc.

Exmerge needs the ExchangeLegacyDN in order to locate the mailbox. This code has good generic code that can be used to latch onto a user object. From there you can easily set any property on the account or extract info about the account.

Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
sInFile = "input.txt"
sOutFile = "mailboxes.txt"
Set oRootDSE = GetObject("LDAP://RootDSE")
sDNSDomain = oRootDSE.Get("defaultNamingContext")
Set oTrans = CreateObject("NameTranslate")
oTrans.Init ADS_NAME_INITTYPE_GC, sDNSDomain
oTrans.Set ADS_NAME_TYPE_1779, sDNSDomain
sNetBIOSDomain = oTrans.Get(ADS_NAME_TYPE_NT4)
sNetBIOSDomain=Left(sNetBIOSDomain,Len(sNetBIOSDomain)-1)
set oFSO = CreateObject("Scripting.FileSystemObject")
set oInTextFile = oFSO.OpenTextFile (sInFile)
set oFSO = CreateObject("Scripting.FileSystemObject")
set oOutTextFile = oFSO.CreateTextFile (sOutFile)
Do
sUser = oInTextFile.ReadLine
oTrans.Init ADS_NAME_TYPE_1779, sNetBIOSDomain
oTrans.Set ADS_NAME_TYPE_NT4, sNetBIOSDomain & "\" & sUser
sUserDN = oTrans.Get(ADS_NAME_TYPE_1779)
set oUser = GetObject ("LDAP://" & sUserDN)
sLegacyExchangeDN = oUser.LegacyExchangeDN
oOutTextFile.Writeline sLegacyExchangeDN
Loop Until oInTextFile.AtEndOfLine = true
wscript.Echo "All Done"

No comments: