Import PSTs to Zimbra Mailboxes Powershell Script

This script I used to import a large amount of PST files to Zimbra mailboxes. The script builds the XML definitions file to be able to use the zcspstimportwizard application in batch mode and then runs the application against each file.

# ============================================================================
# NOTES:
# 1. import pst mapping file
# 2. Get the primary STMP address
# 3. write definitions file
#
# Script must be ran in Exchange Powershell Console, otherwise,
# import Exchange modules
#
# Script calls for a CSV file named filelist.csv containing the column headers:
# file,samaccountname,userprincipalname
# ============================================================================
# set variables
$dc = “dc.local”
$mapfilename = “filelist.csv”
$mapfilepath = “c:\zimbra\zcspstimport\”
$mapfullfilepath = ($mapfilepath + $mapfilename)
$definitionsfolder = “definitions\”
$pstfolder = “F:\”
foreach ($i in (import-csv $mapfullfilepath))
{
# get email address info
# Uncomment next line if source is a mailuser
$mail = get-mailuser -identity $i.userprincipalname -domaincontroller $dc
# Uncomment next line if source is a mailbox
#mail – get-mailbox -identity $map.userprincipal
$definitionsfile = ($mapfilepath + $definitionsfolder + $i.samaccountname + “myfile.xml”)
Write-Host $definitionsfile
Write-Host ($pstfolder + $i.userprincipalname + “\” + $i.file)
#build xml file
$stream = new-object system.IO.StreamWriter($definitionsfile)
$stream.writeline(‘<ZCSImport>’)
$stream.writeline(‘<Server name=”server”>’)
$stream.writeline(‘<serverName>zimbra-mbsarchive-01.phs.corp</serverName>’)
$stream.writeline(‘<port>443</port>’)
$stream.writeline(‘<sslEnabled>1</sslEnabled>’)
$stream.writeline(‘</Server>’)
$stream.writeline(‘<Account name=”user”>’)
$stream.writeline(‘<userName>’ + $i.userprincipalname + “.archive” + ‘</userName>’)
$stream.writeline(‘<organizerEmail>’ + $mail.primarysmtpaddress + ‘</organizerEmail>’)
$stream.writeline(‘</Account>’)
$stream.writeline(‘<Account name=”admin”>’)
$stream.writeline(‘<userName>admin</userName>’)
$stream.writeline(‘<password>g0iokaZHv</password>’)
$stream.writeline(‘</Account>’)
$stream.writeline(‘<Data name=”psts”>’)
$stream.writeline(‘<PST>’ + $pstfolder + $i.userprincipalname + “\” + $i.file + ‘</PST>’)
$stream.writeline(‘</Data>’)
$stream.writeline(‘<Options name=”config1″>’)
$stream.writeline(‘<importDeletedItems>0</importDeletedItems>’)
$stream.writeline(‘<importJunk>0</importJunk>’)
$stream.writeline(‘<importPrivateAppts>1</importPrivateAppts>’)
$stream.writeline(‘<importPartialMails>1</importPartialMails>’)
$stream.writeline(‘<InvalidSSLOk>1</InvalidSSLOk>’)
$stream.writeline(‘<ignorePreviouslyImported>1</ignorePreviouslyImported>’)
$stream.writeline(‘</Options>’)
$stream.writeline(‘<Import Server=”server” TargetAccount=”user” AuthAccount=”admin” AdminPort=”7071″ AdminSSLEnabled=”1″ Data=”psts” Options=”config1″/>’)
$stream.writeline(‘</ZCSImport>’)
$stream.close()
$cmd = ‘C:\Zimbra\ZCSPSTImport-JB\ZCSPSTImportWizard-8.0.2.686.exe’
$param1 = ‘nopause’
#Run ZCSPSTImportWizard
&$cmd “$definitionsfile” “$param1”
wait-process ZCSPSTImportWizard-8.0.2.686 -timeout 3600
}
#exit

NOTES:

To append a file rather than a new file use syntax:

new-object system.IO.StreamWriter("filename.txt",1) or

new-object system.IO.StreamWriter("filename.txt",$false)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s