@echo off
:: Purpose: Batch File to export a users exchange mailbox to pst file
:: Author: Evan Waite
:: Date Created: Nov 03, 2005
:: Date Modified: Nov 04, 2005
:: Comments: Inline Comments for each step. Instructions below
:: Changelog: Nov 03, 2005 - Initial Version
:: Nov 04, 2005 - Integrate with ADModify to handle granting and removing Mailbox rights
::
:: Requirements:
:: You need the following installed on your workstation prior to using the script
:: - Exchange 2003 Admin tools (2000 might work but not tested)
:: - ExMerge.exe from the Exchange 2003 Resource Kit (must be in local Exchange bin folder)
:: - .NET Framework 1.1 Installed
:: - ADModify (http://www.gotdotnet.com/workspaces/workspace.aspx?id=f5cbbfa9-e46b-4a7a-8ed8-3e44523f32e2)
::
:: License:
:: Feel free to use this script in any way but please credit me for my work. Oh and if you make any money, pleaes send BEER!
::
:: Instructions:
:: 1. Modify any parameters (variable names are pretty clear)
:: 2. Manually Run this file with the following syntax or call it from the Hyena tools menu.
:: export-exchange-mailbox.bat username
:: 3. The log file will open after. Check for any errors:: Sets some date variables assuming the MM/DD/YYYY format. These are used in the export log file
set thisMonth=%date:~4,2%
set thisDay=%date:~7,2%
set thisYear=%date:~12,2%
:: Variables
set exportFolder=C:\Exported-Mailboxes
set mailBoxPath=/O=DOMAIN NAME/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=
set exchangeServerName=ExchangeServerName
set exportUserName=%1
set exchangeBinFolder=C:\Program Files\Exchsrvr\bin
set exchMemDLL=exchmem.dll
set exMergeEXE=ExMerge.exe
set sharedToolsPath=\\server\Hyena-Shared\Tools
set legacyExchangeDN=%mailBoxPath%%exportUserName%
set exportMailBoxFile=mailbox-%exportUserName%.txt
set exportSettingFile=exmerge-%exportUserName%.ini
set exportLogFile=exmerge-%exportUserName%-%thisMonth%-%thisDay%-%thisYear%.log
set exportLoggingLevel=1
set textViewerApplication=notepad.exe
:: These are Variables for ADModify
set adminUserName=%USERNAME%
set adModifyToolPath=ADModify
set adModifyToolEXE=ADModcmd.exe
set adModifyToolOptions=-s
set adModifyToolAddAccess=-addtomailboxrights
set adModifyToolRemoveAccess=-removefrommailboxrights
set adModifyToolAddAccessMask=ACE_MB_FULL_ACCESS
set adModifyToolRemoveAccessMask=ALL
set adBaseDN="DC=domain,DC=com"
set adLDAPFilter="(sAMAccountName=%exportUserName%)"
set adDomainName=DOMAIN
:: Debugging
:: If you are having problems, set debugEnabled=1
:: this will keep the exportMailBoxFile, exportSettingFile and run export with verbose logging
set debugEnabled=0
:: If debugging is enabled, bump the exportLoggingLevel to verbose
IF %debugEnabled%==1 set exportLoggingLevel=3
:: Check to make sure everything is in order
:: Check to see if you passed in a username parameter. If not, echo the syntax and exit
IF "%1" == "" (
echo Error: Missing username parameter
echo Please use the following syntax:
echo export-exchange-mailbox.bat username
GOTO EOF
)
:: Check to see if the export folder exists. Tries to create it
IF /I NOT EXIST %exportFolder% (
echo Error: The %exportFolder% folder does not exist
echo Trying to create it
mkdir %exportFolder%
)
:: A second check check to see if the export folder exists. Dies if it doesn't exist.
IF /I NOT EXIST %exportFolder% (
echo Error: The %exportFolder% folder does not exist
echo Please edit this file and set the exportFolder variable
GOTO EOF
)
:: Check to see if the exchange bin folder exists. Dies if it doesn't exist.
IF /I NOT EXIST "%exchangeBinFolder%" (
echo Error: The %exchangeBinFolder% folder does not exist
echo Please edit this file and set the exchangeBinFolder variable
GOTO EOF
)
:: Check to see if exchmem.dll in the exchange bin folder exists. Dies if it doesn't exist.
IF /I NOT EXIST "%exchangeBinFolder%\%exchMemDLL%" (
echo Error: The %exchMemDLL% was not found in %exchangeBinFolder%
echo You may not have the exchange admin tools installed.
echo Please verify you have the tools installed and edit any path settings in this file.
GOTO EOF
)
:: Check to see if ExMerge.exe is in the exchange bin folder. Tries to grab from an alt location
IF /I NOT EXIST "%exchangeBinFolder%\%exMergeEXE%" (
echo Error: The %exMergeEXE% was not found in %exchangeBinFolder%
echo Trying to copy the file from an alternate location
copy %sharedToolsPath%\%exMergeEXE% "%exchangeBinFolder%\"
IF ERRORLEVEL 1 (
echo Can not seem to find %sharedToolsPath%\%exMergeEXE%
echo Exiting the batch file
GOTO EOF
)
IF ERRORLEVEL 0 (
echo Successfully copied %exMergeEXE% into %exchangeBinFolder%
echo Continuing to next step
)
)
:: Check to see if the local ADModify folder exists. Tries to create it
IF /I NOT EXIST %exportFolder%\%adModifyToolPath% (
echo Error: The %exportFolder%\%adModifyToolPath% folder does not exist
echo Trying to create it
mkdir %exportFolder%\%adModifyToolPath%
)
:: A second check check to see if the local ADModify folder exists. Dies if it doesn't exist.
IF /I NOT EXIST %exportFolder%\%adModifyToolPath% (
echo Error: The %exportFolder%\%adModifyToolPath% folder does not exist
echo Please edit this file and set the adModifyToolPath variable
GOTO EOF
)
:: Check to see if ADModcmd.exe is in the local ADModify folder. Tries to grab from an alt location
IF /I NOT EXIST %exportFolder%\%adModifyToolPath%\%adModifyToolEXE% (
echo Error: The %adModifyToolEXE% was not found in %exportFolder%\%adModifyToolPath%
echo Trying to copy the file from an alternate location
copy %sharedToolsPath%\%adModifyToolPath% %exportFolder%\%adModifyToolPath%
IF ERRORLEVEL 1 (
echo Can not seem to find %sharedToolsPath%\%adModifyToolPath%\%adModifyToolEXE%
echo Exiting the batch file
GOTO EOF
)
IF ERRORLEVEL 0 (
echo Successfully copied %adModifyToolEXE% and required files into %exportFolder%\%adModifyToolPath%
echo Continuing to next step
)
)
:: Check to see if the exportSettingFile file exists. If true, remove it
IF /I EXIST %exportFolder%\%exportSettingFile% (
echo Removing old %exportFolder%\%exportSettingFile% file
del %exportFolder%\%exportSettingFile%
)
:: Check to see if the exportMailBoxFile file exists. If true, remove it
IF /I EXIST %exportFolder%\%exportMailBoxFile% (
echo Removing old %exportFolder%\%exportMailBoxFile% file
del %exportFolder%\%exportMailBoxFile%
)
:: Check to see if the renamed exportPST file exists. If true, dies to avoid overwriting it
IF /I EXIST %exportFolder%\%exportUserName%-existing-file-renamed-on-%thisMonth%-%thisDay%-%thisYear%.PST (
echo Error: Found an existing renamed version of the exported file
echo Please clean up these files and rerun
GOTO EOF
)
:: Check to see if the exportPST file already exists. If true, renames it
IF /I EXIST %exportFolder%\%exportUserName%.PST (
echo Renaming old %exportFolder%\%exportUserName%.PST file
rename %exportFolder%\%exportUserName%.PST %exportUserName%-existing-file-renamed-on-%thisMonth%-%thisDay%-%thisYear%.PST
)
:: Grant the current admin full rights to the User Mailbox using ADModify
echo Giving %adDomainName%\%adminUserName% full rights to %exportUserName% mailbox
%exportFolder%\%adModifyToolPath%\%adModifyToolEXE% -dn %adBaseDN% -f %adLDAPFilter% %adModifyToolOptions% %adModifyToolAddAccess% %adDomainName%\%adminUserName% %adModifyToolAddAccessMask%
:: Write the required files
:: Exported Mailboxes Files which contains the fully qualified legacyExchangeDN name
echo Writing Mailbox file
echo %legacyExchangeDN%>> %exportFolder%\%exportMailBoxFile%
:: Exchange Merge Utility Settings File (with only the required settings)
echo Writing ExMerge Settings File
echo [EXMERGE]>> %exportFolder%\%exportSettingFile%
echo LoggingLevel=%exportLoggingLevel% >> %exportFolder%\%exportSettingFile%
echo LogFileName=%exportFolder%\%exportLogFile%>> %exportFolder%\%exportSettingFile%
echo DataDirectoryName=%exportFolder%>> %exportFolder%\%exportSettingFile%
echo SourceServerName=%exchangeServerName%>> %exportFolder%\%exportSettingFile%
echo FileContainingListOfMailboxes=%exportFolder%\%exportMailBoxFile%>> %exportFolder%\%exportSettingFile%
:: Run the export using the files we just wrote
echo Exporting Email
"%exchangeBinFolder%\%exMergeEXE%" -B -F %exportFolder%\%exportSettingFile%
:: Opens the export log file in notepad
start %textViewerApplication% %exportFolder%\%exportLogFile%
:: Remove the admins full rights to the User Mailbox using ADModify
echo Removing %adDomainName%\%adminUserName% full rights to %exportUserName% mailbox
%exportFolder%\%adModifyToolPath%\%adModifyToolEXE% -dn %adBaseDN% -f %adLDAPFilter% %adModifyToolOptions% %adModifyToolRemoveAccess% %adDomainName%\%adminUserName% %adModifyToolRemoveAccessMask%
:: Clean up the exportMailBoxFile and exportSettingFile if debugging in not enabled
IF NOT %debugEnabled%==1 (
del %exportFolder%\%exportMailBoxFile%
del %exportFolder%\%exportSettingFile%
)
:: Clean up the local copy of ADModify
IF /I EXIST %exportFolder%\%adModifyToolPath% (
echo Removing %exportFolder%\%adModifyToolPath% folder
rmdir /S /Q %exportFolder%\%adModifyToolPath%
)
:EOF
echo Done!