Monday, January 27, 2014

Balance Mailbox DBs Across All Public Folder DBs

Doing a quick inventory of my mailbox databases recently, I found that 80% of my mailbox dbs, were pointing to only 2 of my 6 PF dbs. This script will distribute your mailbox dbs evenly across your public folder dbs.

One concept that I played with in this script is using hash tables. Hash tables allow you to specify a string value to an array, and it returns another entry.

For example, the code:

$pfdbs = get-publicfolderdatabase
$DBSize = @{}
$pfdbs | %{$dbsize[$_.identity.tostring()] = 0}
$MDBG | %{$dbsize[([string]$_.name)] = $_.count}

when completed, will be a hash table of PF DBs and the # of mailbox dbs pointing to each.

$DBSize{"PF-DB01"}  returns the value of 8



This later on in the script allows me to both track how many dbs are pointing to a specific PF db.

$dbsize[$destination.identity.tostring()] += 1
$dbsize[$mdbgp.name] -= 1

The code does assume 2007 PF dbs exist in the environment, so when we decommission that environment, I'll need to rerun the script without the -includepreExchange2010 switch.


<#
.SYNOPSIS
   Distribute mailbox dbs across all Public folder dbs. 
#>

$pfDbs = get-publicfolderdatabase -includepreexchange2010
$mbdbs = get-mailboxdatabase 

$mbc = $mbdbs.count
$pfc = $pfDbs.count

[int] $adbc = ($mbc / $pfc)

$MDBG = $mbdbs | group publicfolderdatabase
$moveOff = $MDBG | ?{$_.count -gt $adbc}
$DBSize = @{}
$pfdbs | %{$dbsize[$_.identity.tostring()] = 0}
$MDBG | %{$dbsize[([string]$_.name)] = $_.count}

if ($moveOff -eq $null) {
 Write-Host "nothing to do."
 $dbsize
 break}
ForEach ($mdbgp in $moveOff) {
 $MoveCount = $mdbgp.count - $adbc
 forEach ($db in $MDBGP.group[0..$moveCount]) {
  $destinations = $pfdbs | ?{$dbsize[$_.identity.tostring()] -lt $adbc}
  if ($destinations -is [array]) {
   $destination = $destinations[0]
  } elseif ($destinations -eq $null) {
   Write-Host "no destinations under average, $adbc"
   break
  } else { 
   $destination = $destinations
  }
  if ($destination -ne $null -and $destination -ne $mdbgp.name) {
   Write-Host $db,$mdbgp.name,$destination
   Set-mailboxdatabase -identity $db -publicfolderdatabase $destination
   $dbsize[$destination.identity.tostring()] += 1
   $dbsize[$mdbgp.name] -= 1
  }
 }
}
$dbsize

No comments:

Post a Comment