{"id":889,"date":"2018-03-12T21:46:13","date_gmt":"2018-03-12T21:46:13","guid":{"rendered":"http:\/\/ihni.uk\/?p=889"},"modified":"2018-03-12T21:46:13","modified_gmt":"2018-03-12T21:46:13","slug":"empty-redistribute-oversubscribed-exchange-2016-mailboxdb","status":"publish","type":"post","link":"https:\/\/ihni.uk\/?p=889","title":{"rendered":"Empty &#038; Redistribute Oversubscribed Exchange 2016 MailboxDB"},"content":{"rendered":"<p>In a newly created Exchange 2016 on premise installation I inherited support of, we had 60 MailboxDBs.<\/p>\n<p>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&#8230;<!--more--><\/p>\n<p>So I hacked the following together to empty and move the mail to the other MailboxDBs.<\/p>\n<p>Once this script has completed, the Mailbox can be deleted and whitespace freed up to release disk space.<\/p>\n<p>[cc lang=&#8217;powershell&#8217;]<\/p>\n<p>######################################################################<br \/>\n#<br \/>\n# PowerShell Script to Empty an Over utilised Mailbox DB and spread the<br \/>\n# load over the remaining Exchange Environment.<br \/>\n#<br \/>\n# Please be aware that this will totally\u00a0empty the chosen DB.<br \/>\n#<br \/>\n#######################################################################<\/p>\n<p>Add-PSSnapin Microsoft.Exchange.Management.Powershell.snapin;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Load Exchange\/AD Cmndlets<\/p>\n<p>#$DBs = Get-MailboxDatabase | Select Name | Sort-Object Name<br \/>\n$OSMBDB = Read-Host &#8220;Please Enter the Oversubscribed MailboxDB name: &#8221; \u00a0# Get OS DB from user<br \/>\n$mailboxes = Get-Mailbox -database $OSMBDB | Select name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Retreive Mailboxes from MailboxDB<br \/>\n$mailboxes = $mailboxes.name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # List Mailboxes only by Name<br \/>\n$Count = $mailboxes.count\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Count Mailboxes<br \/>\n$ExcludeDB = $OSMBDB -replace &#8216;[^0-9]&#8217;, &#8221;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Set Over Subscribed DB to be excluded from script<br \/>\n$ExcludeDB =[int]$ExcludeDB\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Changes OSDB to an Integer<br \/>\n$DBNo = 0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Sets Count to zero<\/p>\n<p>Write-Host &#8220;There are $count Mailboxes in $OSMBDB`n&#8221;<\/p>\n<p>ForEach ($mailbox in $mailboxes){<br \/>\n$DBno++\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Incremement Count by 1<br \/>\nIf($dbno -eq $ExcludeDB){$dbno++}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Skip Excluded DB<br \/>\nElseIf ($dbno -gt &#8220;59&#8221;){$DBNo = 1}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Skip DB 60 and Go back to the Start<br \/>\n# Prompt for user input to run 10 jobs at a time.This can be changed to be automated, by waiting for all &#8220;Get-MoveRequests -Status *Completed*or*Failed*&#8221; Can provide Script if needed.<br \/>\nElseIf ($dbno -eq &#8220;11&#8221; -or $dbno -eq &#8220;21&#8221; -or $dbno -eq &#8220;31&#8221; -or $dbno -eq &#8220;41&#8221; -or $dbno -eq &#8220;51&#8221; -or $dbno -eq &#8220;59&#8221;){Pause}<br \/>\n$dbnoS =$dbno.ToString(&#8220;00&#8243;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Convert Int-String<br \/>\nWrite-Verbose &#8221; Preparing to move $mailbox to DB$dbnoS&#8230;&#8221;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Display Prompt, can be output to Log if needed.<br \/>\n#New-MoveRequest -Identity $mailbox -TargetDatabase &#8220;DB$DBNoS&#8221;\u00a0\u00a0\u00a0\u00a0\u00a0 # Command to Move Mailboxes<\/p>\n<p>}<\/p>\n<p>[\/cc]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a newly created Exchange 2016 on premise installation I inherited support of, we had&hellip;<\/p>\n","protected":false},"author":1,"featured_media":988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,19,11],"tags":[32,53],"class_list":["post-889","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-exchange","category-powershell","category-scripts","tag-exchange","tag-mailboxes"],"jetpack_featured_media_url":"https:\/\/ihni.uk\/wp-content\/uploads\/2020\/02\/Powershell.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=889"}],"version-history":[{"count":0,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/889\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}