Defragment the Exchange Information Store

Submitted By: Matthew Jones

Language: Batch

Description: Defragments the Exchange information store.

Script Code

@Echo off
:: ExMaint Version 1.2 
:: Matthew Jones - 
:: Script must include Exchagne Server name as part of parameter {ie: Scriptname.cmd Servername}
::
:: Purpose: Take Exchange Information Store off-line - Backup EDB and STM file.  Perform Defrag.  Reboot Server
::
:: Why: So I can get some sleep while the Exchange Store does a poor mans defrag.
::
:: Assumpsion 1: The Exchange program is installed to c:\program files\exchsrvr {for eseutil access} 
:: Assumption 2: You have enough space in the backup path to accomodate the store backup.
:: Assumption 3: You have the shutdown utility in a common path {i.e: winnt\system32}.
CLS

:: What directory do you want the report to generate in?
set ReportPath=c:\scripts\reports\
if not exist %reportpath% mkdir c:\scripts\reports

set ReportName=%reportpath%%1.txt

:: location to back up EDB and STM files
set backuppath=d:\exchbup\

:: Create backup path if it does not exist
if not exist %backuppath% mkdir %backuppath%

:: location where Exchange Store DB resides
set datalocation=d:\InfoStore\

:: Exchange Store Name
set dataname=priv1.edb

:: location where Exchange Store STM resides
set streamlocation=d:\InfoStore\

:: Exchange STM filename
set streamname=priv1.stm

date /t  > %ReportName%
time /t >> %ReportName%

echo Server Name is : %1 >> %ReportName%
echo Report Path    : %reportpath% >> %ReportName%
echo Report Name    : %reportname% >> %ReportName%
echo                  ---------------- >> %ReportName%
echo Backup Path    : %backuppath% >> %ReportName%
echo Data Location  : %datalocation% >> %ReportName%
echo Data Name      : %dataname% >> %ReportName%
echo Stream Location: %streamlocation% >> %ReportName%
echo Stream Name    : %streamname% >> %ReportName%

echo Server Name is : %1
echo Report Path    : %reportpath%
echo Report Name    : %reportname%
echo                  ----------------
echo Backup Path    : %backuppath%
echo Data Location  : %datalocation%
echo Data Name      : %dataname%
echo Stream Location: %streamlocation%
echo Stream Name    : %streamname%


if %1 == "" goto :errorfound

set ExchServer=%1
echo The Server Name is: %ExchServer%

:StartJob
Echo Stopping the Exchange Store on %ExchServer% 
Echo Stopping the Exchange Store on %ExchServer% >> %ReportName%

net stop "Microsoft Exchange Information Store" >> %ReportName%
Echo Exchange Store Stopped 
Echo Exchange Store Stopped >> %ReportName%
date /t  > %ReportName%
time /t >> %ReportName%

Echo Delete Old Copy - Backup Production Info Store
if exist %BackupPath%%dataname% del %BackupPath%%dataname%
if exist %BackupPath%%streamname% del %BackupPath%%streamname%
Echo Delete Old Copy - Backup Production Info Store >> %ReportName%

Echo Copy STM File to %backuppath%.
Copy %streamlocation%%streamname% %backuppath%
Echo Copy of STM File Completed. >> %ReportName%
Echo Copy of STM File Completed. 

Echo Copy %dataname% to %backuppath%. >> %ReportName%
Echo Copy %dataname% to %backuppath%. 
Copy %datalocation%%dataname% %backuppath%
Echo Copy of STM File Completed. >> %ReportName%
Echo Copy of STM File Completed. 

if errorlevel = 0 goto :STARTESEUTIL

:failcopy
Echo The Copy process failed - Process aborted! 
Echo The Copy process failed - Process aborted! >> %ReportName%
goto :ENDMAINT
 
:STARTESEUTIL
Echo Starting ESEUTIL
Echo Starting ESEUTIL>> %ReportName%
date /t >> %ReportName%
time /t >> %ReportName%

"c:\program files\exchsrvr\bin\eseutil.exe" /d %datalocation%%dataname%
Echo ESEUTIL Defragmentation Completed >> %ReportName%
Echo ESEUTIL Defragmentation Completed 
date /t >> %ReportName%
time /t >> %ReportName%




:StartServices

Echo Starting Microsoft Exchange Information Store
Echo Starting Microsoft Exchange Information Store >> %ReportName%
net start "Microsoft Exchange Information Store" >> %ReportName%
date /t >> %ReportName%
time /t >> %ReportName%

echo Rebooting Server >> %ReportName%
echo Rebooting Server
shutdown /l /r /t:20
goto :ENDMAINT


:errorfound
Echo You did not set a Server name as a parameter of the script.
Echo Please use the Exchange Servername as a parameter in the script.

:ENDMAINT
Echo Script Ended >> %ReportName%
date /t >> %ReportName%
time /t >> %ReportName%
:: notepad %ReportName%


Note: Not all scripts run on all versions of Windows.

For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts or on the Script Center, please contact scripter@microsoft.com.

Disclaimer

This script is not supported under any Microsoft standard support program or service. The script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the script or documentation, even if Microsoft has been advised of the possibility of such damages.


Top of pageTop of page