Empty & Redistribute Oversubscribed Exchange 2016 MailboxDB

Empty & Redistribute Oversubscribed Exchange 2016 MailboxDB

In a newly created Exchange 2016 on premise installation I inherited support of, we had 60 MailboxDBs.

Users were supposed to be provisioned into these evenly from the old environment, but they were lumped into two of the DBs rather than evenly spread…

So I hacked the following together to empty and move the mail to the other MailboxDBs.

Once this script has completed, the Mailbox can be deleted and whitespace freed up to release disk space.

[cc lang=’powershell’]

######################################################################
#
# PowerShell Script to Empty an Over utilised Mailbox DB and spread the
# load over the remaining Exchange Environment.
#
# Please be aware that this will totally empty the chosen DB.
#
#######################################################################

Add-PSSnapin Microsoft.Exchange.Management.Powershell.snapin;           # Load Exchange/AD Cmndlets

#$DBs = Get-MailboxDatabase | Select Name | Sort-Object Name
$OSMBDB = Read-Host “Please Enter the Oversubscribed MailboxDB name: ”  # Get OS DB from user
$mailboxes = Get-Mailbox -database $OSMBDB | Select name                # Retreive Mailboxes from MailboxDB
$mailboxes = $mailboxes.name                                            # List Mailboxes only by Name
$Count = $mailboxes.count                                               # Count Mailboxes
$ExcludeDB = $OSMBDB -replace ‘[^0-9]’, ”                              # Set Over Subscribed DB to be excluded from script
$ExcludeDB =[int]$ExcludeDB                                             # Changes OSDB to an Integer
$DBNo = 0;                                                              # Sets Count to zero

Write-Host “There are $count Mailboxes in $OSMBDB`n”

ForEach ($mailbox in $mailboxes){
$DBno++                                                             # Incremement Count by 1
If($dbno -eq $ExcludeDB){$dbno++}                                   # Skip Excluded DB
ElseIf ($dbno -gt “59”){$DBNo = 1}                                  # Skip DB 60 and Go back to the Start
# Prompt for user input to run 10 jobs at a time.This can be changed to be automated, by waiting for all “Get-MoveRequests -Status *Completed*or*Failed*” Can provide Script if needed.
ElseIf ($dbno -eq “11” -or $dbno -eq “21” -or $dbno -eq “31” -or $dbno -eq “41” -or $dbno -eq “51” -or $dbno -eq “59”){Pause}
$dbnoS =$dbno.ToString(“00″)                                        # Convert Int-String
Write-Verbose ” Preparing to move $mailbox to DB$dbnoS…”          # Display Prompt, can be output to Log if needed.
#New-MoveRequest -Identity $mailbox -TargetDatabase “DB$DBNoS”      # Command to Move Mailboxes

}

[/cc]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *