# encoding: ascii
# api: powershell
# title: 
# description: evac mailboxes
# version: 0.1
# type: script
# license: CC0
# function: Send-MessageSuccess
# x-poshcode-id: 2417
# x-archived: 2010-12-24T06:51:32
#
#
<# Aaron Anderson - Evacuate an Exchange 2007 database. 
Offline defrags are not a good option when dealing with large databases that can take 10 hours to copy around. 
I can't have 300 users offline for that long. Specifiy the DB you want to emtpy with the $EvacDB variable. The script will find the smallest 
database and then move 2 random mailboxes to that location. It will determine the smallest database after each pair of mailboxes is moved and
maintain that databases are not filled beyond 80gb. This is useful for a number of reasons. If you have 100GB of mailboxes to move, you may not have 
that much free space in any one DB. #>

function Send-MessageSuccess {
Send-MailMessage -To aaron@domain.com -From aaron@domain.com -Subject "Move-Mailbox COMPLETE" -Body "All mailboxes have been evacuated." -SmtpServer smtp201;
}
function Send-MessageFailure {
Send-MailMessage -To aaron@domain.com -From aaron@domain.com -Subject "Move-Mailbox FAILURE" -Body "There is a problem with the script." -SmtpServer smtp201;
}

# Find all databases (except sxmb201\sg01\db01 Cisco Unity System Mailboxes are stored here. Don't monkey with it.)
$databases = Get-MailboxDatabase | Where-Object {$_.Identity -notlike "sxmb201\sg01\db01"} 

# Define which database to evacuate
$EvacDB = "SXMB201\SG04\DB04"

 do {
	   
	foreach ($database in $databases) 
{ 
# Get the database size summing the size of all mailboxes and "Deleted Items" in the database 
$dbsize = (Get-MailboxStatistics -Database $database.Identity | Measure-Object -Property TotalItemSize,TotalDeletedItemSize -Sum).Sum 

# Compare the sizes to find the smallest DB 
if (($dbsize -lt $smallestdbsize) -or ($smallestdbsize -eq $null)) 
{ 
$smallestdbsize = $dbsize 
$smallestdb = $database 
} 
} 
 
# Make sure we have enough space
$SmallestDBSize = ((Get-MailboxStatistics -database $smallestdb | Measure-Object -Property TotalItemSize,TotalDeletedItemSize -Sum | Select-Object Sum |Measure-Object -Property Sum -Sum).Sum.ToString() /1mb)

if ($SmallestDBSize -lt 81920) {Write-Host "There is enough space, script will proceed"}
else {Write-Host "There is not enough space, script will stop";break}
	   
# Check that there are mailboxes left. Move or end script.
if ((Get-Mailbox -Database $EvacDB) -eq $null) {Write-Host "No more mailboxes left to move, take a nap.";Send-MessageSuccess;break} 
else {Get-Mailbox -Database $EvacDB | Get-Random -Count 2 | Move-Mailbox -TargetDatabase $smallestdb -Confirm:$false}

} while ((Get-Mailbox -Database $EvacDB) -ne $null)
