Please consider donating: https://www.corelan.be/index.php/donate/


4,803 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.

Comments are closed.

Corelan Training

We have been teaching our win32 exploit dev classes at various security cons and private companies & organizations since 2011

Check out our schedules page here and sign up for one of our classes now!

Donate

Want to support the Corelan Team community ? Click here to go to our donations page.

Want to donate BTC to Corelan Team?



Your donation will help funding server hosting.

Corelan Team Merchandise

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

Protected by Copyscape Web Plagiarism Tool

Corelan on Slack

You can chat with us and our friends on our Slack workspace:

  • Go to our facebook page
  • Browse through the posts and find the invite to Slack
  • Use the invite to access our Slack workspace
  • Categories