4,814 views
Exchange 2007 : Powershell script to select optimal database for a new mailbox
If you have multiple Storage Groups / Databases on your Exchange 2007 server, you may want to try to spread your mailboxes over all databases. You could use your “gut feeling” and/or select a database at random, or you could use a simple script to select the “best” database for hosting a new mailbox.
The following script will query all databases, find the total size and number of mailboxes, and will then prompt the most optimal database based on average size per mailbox in the databases.
# # Powershell Script written by Peter Van Eeckhoutte # # Purpose : select the most optimal database for # hosting a new mailbox in Exchange 2007 # taking into account that mailbox data should # be spread across multiple databases as much as possible # # http://www.corelan.be:8800 # # # Load assembly [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $Databases = Get-MailboxDatabase -status | sort-object Name $lowestavgsize=0 $optimaldb="" foreach ($Database in $Databases) { write-host "Database : $Database" write-host "--------------------------------------------------------------" if ($Database.Mounted -eq "True") { $dbstats = Get-MailboxStatistics -database $Database $totaldbsize = 0 $totalnrofmb = 0 $avgmbsize = 0 foreach ($dbstat in $dbstats) { $totalnrofmb++ $totaldbsize = $totaldbsize + $dbstat.TotalItemSize.Value.ToMB() } if ($totalnrofmb -gt 0) { $avgmbsize = $totaldbsize/$totalnrofmb } $avgmbsize=[math]::round($avgmbsize, 2) write-host " Total Database Size : $totaldbsize Mb" write-host " Number of mailboxes : $totalnrofmb " write-host " Average mailbox size : $avgmbsize Mb" if (($lowestavgsize -eq 0) -and ($optimaldb -eq "")) { $lowestavgsize = $avgmbsize $optimaldb = $Database } else { if ($avgmbsize -lt $lowestavgsize) { $optimaldb = $Database $lowestavgsize = $avgmbsize } } } else { write-host "Database : $Database : not mounted" write-host "--------------------------------------------------------------" } write-host "" } #find optimal database [System.Windows.Forms.MessageBox]::Show("Database :`n$optimaldb`n Avg mailbox size : $lowestavgsize Mb","Optimal Database")
You can either run this powershell directly from a Powershell command line, or you can create a batch file to run the script :
Some assumptions :
- The powershell script is called selectoptimaldb.ps1, saved under c:\scripts
- Exchange is installed at D:\Program Files\Microsoft\Exchange Server
selectoptimaldb.bat
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-PSConsoleFile "D:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"
-command ". 'C:\Scripts\selectoptimaldb.ps1'"
(put everything on one line, save the file, put a shortcut on your desktop, and you’re ready to go)
© 2009, Peter Van Eeckhoutte (corelanc0d3r). All rights reserved.