Home > Scripts > Powershell > Exchange 2007 : Powershell script to select optimal database for a new mailbox

3,634 views | This page as PDF | Print This Post Print This Post

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, Corelan Team (corelanc0d3r). All rights reserved.

Related Posts:

Leave a Reply

Corelan on IRC

You can chat with us and our friends on #corelan (freenode IRC)

Corelan Team Merchandise

You can support Corelan Team by donating or purchasing items from the official Corelan Team merchandising store.


Corelan Team Wallpaper / Exploit dev cheatsheet : Download here

Corelan Live training

Starting in 2011, Corelan will be teaching live win32 exploit dev bootcamp classes at various security cons.

You can read more about the training and schedules here

Categories

Copyright Dispute with Peter Van Eeckhoutte resolved.
Click here for our Settlement Notice