Please take a moment to read http://bit.ly/demandglobalchange, to help share the message and support the initiative to tell our leaders to focus on addressing the global world problems, instead of complaining about the effects of their lack of leadership. Be a leader yourself, and share this with as many people as possible. #demandglobalchange // https://www.facebook.com/demandglobalchange



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


4,358 views | This page as PDF

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:

Comments are closed.

Corelan Live training

Since 2011, Corelan GCV has been teaching live win32 exploit dev classes at various security cons and private companies & organizations.

You can read more about the training and schedules here

Demand Global Change

The world needs your help !

Please take a few moments to read the "Demand Global Change Call For Action" document at
http://bit.ly/demandglobalchange
Read the full document at
http://bit.ly/demandglobalchange_full and share the message with as many people as possible.

Like the Facebook page, and SHARE it with everyone you know.



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.

Protected by Copyscape Web Plagiarism Tool

Corelan Team Merchandise

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

Corelan on IRC

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

Categories