PowerShell – Move Active Mailboxes (Exchange 2010 DAG)


The below script allows you to migrate all active mailboxes from one mailbox server to another within the same Exchange 2010 DAG.


###############################################################################
#                                                                             #
# ****************** CREATED BY MAURICE DALY ON 06/01/2014 ****************** #
#                                                                             #
# Move Active Mailbox Server                                                  #
#                                                                             #
# Version 1.0                                                                 #
#                                                                             #
# THIS SCRIPT IS USED AT YOUR OWN RISK. I ACCEPT NO RESPONSIBILITY FOR ANY    #
# ISSUES ARRISING FROM IT                                                     #
#                                                                             #
###############################################################################

$Credential = Get-Credential
$DC = $env:LOGONSERVER.Substring(2)

#Initiate Remote PS Session to local DC
$ADPowerShell = New-PSSession -ComputerName $DC -Authentication Negotiate -Credential $Credential

# Import-Module ActiveDirectory
Invoke-Command -Session $ADPowerShell -scriptblock {import-module ActiveDirectory}
Import-PSSession -Session $ADPowerShell -Module ActiveDirectory -AllowClobber -ErrorAction Stop

# Retrieve AD Details
$ADDetails = Get-ADDomain
$Domain = $ADDetails.DNSRoot

write-host "This script automates the movement of mailboxes for maintenance purposes"
Write-Host ""
Write-Host "Connecting to Microsoft Exchange Environment....."
$ExchangeServer = Get-ADObject -Filter "(ServicePrincipalNAme -like 'IMAP*')" -SearchBase (Get-ADDomain).DistinguishedName.tostring()  -Properties ServiceDNSName, ServiceClassName | where name -NotLike *SDK* | ForEach-Object {Write-Output $($_.Name + "." + $Domain)} | Get-Random
$ExchangePowerShell = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchangeServer/Powershell
Import-PSSession $ExchangePowerShell -AllowClobber
cls
Write-Host -ForegroundColor Yellow "Current mailbox locations:"
Get-MailboxDatabase | ft name, servername
Write-Host "Available mailbox servers:"
Get-MailboxServer | ft name, databaseavailabilitygroup
$MailboxTarget = Read-Host "Please enter the name of the server you wish to move active databases to"
$AppChoice = ""
 while ($AppChoice -notmatch "[Y|N]"){
     Write-Host ""
     Write-Host -ForegroundColor Yellow "[Y] YES" 
     Write-Host -ForegroundColor Yellow "[N] NO"
     Write-Host ""
     $AppChoice = read-host "Are you sure you wish to move all active mailboxes to server $MailboxTarget"
     }
     if ($AppChoice -eq "Y"){
        Get-MailboxDatabase | sort name | ForEach-Object {
        Move-ActiveMailboxDatabase -Identity $_.name -ActivateOnServer $MailboxTarget -MountDialOverride 'None' -Confirm:$False
     }
     }
     if ($AppChoice -eq "N"){
     Write-Host "Mailbox move cancelled."
     }




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s