Friday, June 11, 2010

Mailbox Migration to Exchange 2010 (PowerShell)

As stated in my previous post, I am no scripting guru or will I ever claim to be.  My primary purpose for creating this script is to force myself to learn about PowerShell scripting. 

I am sure that there is a more efficient or better approach here, so please leave comments and let me know.

The following script is written to migrate mailboxes (from Exchange 2003, 2007, or 2010) to an Exchange 2010 database.  This script could easily be modified if your target database was on Exchange 2007, but for now it is just written for Exchange 2010.

##########################################################################
# This script is for migrating mailboxes to Exchange 2010
# There is no warranty, use at your own risk
# Author: Tim Harrington
http://HowDoUC.blogspot.com
#
# Some of the methods in this script rely on Quest AD PowerShell Commands
# Free download at
http://www.quest.com/powershell/activeroles-server.aspx
##########################################################################

# Initial Menu to select method of selecting mailboxes to move
#
Write-Host " "
Write-Host "1 = Move mailboxes by user name"
Write-Host "2 = Move mailboxes by security group"
Write-Host "3 = Move mailboxes by database"
Write-Host "4 = Move mailboxes by OU"
Write-Host " "
Write-Host "Full Report = Run Report -> c:\temp\FullMoveReport.txt"
Write-Host "Error Report = Run Report -> c:\temp\ErrorMoveReport.txt"
Write-Host " "
$Method = Read-Host "Please select an option from the list above?"
#
# Method 1 = Move mailboxes by user name
If ($Method -eq 1)
{
Write-Host "You have choosen to migrate mailboxes by specifying the user name"
Get-MailboxDatabase
$TargetDatabaseName = Read-Host "Type the database that you want to migrate user(s) to"
$UserName = Read-Host "Type the user name that will be migrated"
Write-Host ("Moving user: " +$UserName +" to database: " +$TargetDatabaseName)
New-MoveRequest -identity $UserName -TargetDatabase $TargetDatabaseName
}

# Method 2 = Move mailboxes by security group
If ($Method -eq 2)
{
Write-Host " "
Write-Host "You have choosen to migrate mailboxes by specifying an AD security group"
Write-Host " "
Write-Host "This script relies on Quest AD PowerShell Commands"
Write-Host "Download at http://www.quest.com/powershell/activeroles-server.aspx"
Write-Host " "
Add-PSSnapin Quest.ActiveRoles.ADManagement
$ADSecGroupName = Read-Host "Enter AD security group that you want to migrate"
Get-QADUser -MemberOf $ADSecGroupName | ft
Write-Host "The above list of users will get migrated!!"
Write-Host " "
Get-MailboxDatabase | ft
$TargetDatabaseName = Read-Host "Type the database that you want to migrate user(s) to"

#Loop through each mailbox
foreach ($mailbox in (Get-QADUser -MemberOf $ADSecGroupName))
{$displayname = $mailbox.Displayname
New-MoveRequest -Identity $displayname -TargetDatabase $TargetDatabaseName}
}
#
# Method 3 = Move mailboxes by database
If ($Method -eq 3)
{
Write-Host "You have choosen to migrate mailboxes from one database to another database"
Get-ExchangeServer | ft
$SourceServer = Read-Host "What is the source server from list above?"
Get-MailboxDatabase -Server $SourceServer | ft
$SourceDatabaseName = Read-Host "What is the SOURCE database from list above? (Type exactly from list)"
Get-MailboxDatabase | ft
$TargetDatabaseName = Read-Host "What is the TARGET database from list above? (Type exactly from list)"
Write-Host ("Moving users from source database: " +$SourceDatabaseName +" to target database: " +$TargetDatabaseName)
Get-Mailbox -Database $SourceDatabaseName
New-MoveRequest -TargetDatabase $TargetDatabaseName
}
#
# Method 4 = Move mailboxes by OU
If ($Method -eq 4)
{
Write-Host " "
Write-Host "You have choosen to migrate mailboxes by specifying an AD OU"
Write-Host " "
Write-Host "This script relies on Quest AD PowerShell Commands"
Write-Host "Download at http://www.quest.com/powershell/activeroles-server.aspx"
Write-Host " "
Add-PSSnapin Quest.ActiveRoles.ADManagement
Get-QADObject -Type OrganizationalUnit | ft
$ADOUName = Read-Host "Enter AD OU that you want to migrate"
Get-QADUser -Searchroot $ADOUName -SearchScope OneLevel | ft
Write-Host "The above list of users will get migrated!! (This does not include sub-OUs)"
Write-Host " "
Get-MailboxDatabase | ft
$TargetDatabaseName = Read-Host "Type the database that you want to migrate user(s) to"

#Loop through each mailbox
foreach ($mailbox in (Get-QADUser -Searchroot $ADOUName))
{$displayname = $mailbox.Displayname
New-MoveRequest -Identity $displayname -TargetDatabase $TargetDatabaseName}
}
#
# Save off full report
If ($Method -eq "Full Report")
{
Get-MoveRequest | Get-MoveRequestStatistics | Select DisplayName, Status, TotalItemSize, TotalMailboxItemCount, PercentComplete, BytesTransferred, ItemsTransferred | Out-File -FilePath "c:\temp\FullMoveUserLog.txt" -append -noClobber
}

# Save off error report
If ($Method -eq "Error Report")
{
Get-MoveRequest | Get-MoveRequestStatistics where {$_.Status -like "*error*"} | Select DisplayName, Status, TotalItemSize, TotalMailboxItemCount, PercentComplete, BytesTransferred, ItemsTransferred | Out-File -FilePath "c:\temp\ErrorMoveUserLog.txt" -append -noClobber
}
#############################################################################

Hope this helps some folks out there.  Comments are welcome.

3 comments:

  1. Great, thanks for sharing the script to migrate mailbox from exchange and migrate the entire or selected mailbox data to Exchange Online from your Exchange server. But I tested the automate exchange migrator tool from http://www.lepide.com/exchangemigrator/ that provides the facilitate to migrate to-and-fro between exchange server and complete migration including all items of user mailboxes and allows to migrations of Intra-forest and cross forest exchange server.

    ReplyDelete
  2. Thanks for sharing this information. But the article is targeting migration into Exchange 2010 mailboxes. But I tried the above mentioned way too for migration into Exchange 2013 from 2007, but couldn't get success.
    Therefore I went for a utility of Exchange Migration. But in the process of finding a utility, I came to know that it isn't possible to migrate mailboxes directly from one exchange server to another. Therefore I followed the way to Export Exchange mailboxes into PST and then import all PST files into another exchange server, and then configured all services like OWA, ECP etc in a proper manner.
    It was really an effortless way of migration with export & import mailbox technique by using Exchange recovery tool.

    ReplyDelete