Monday, 19 September 2011 05:17

Automating the Linked Mailbox Creation – Cross Forest Migration

Written by

In case of cross forest migration, most of us will plan the mailbox and account migration together to avoid complexity. However, there are situations where we plan to go one by one (all the user account migration first and then the mailbox migration). In my case, I chose to do the user account migration due to some dependency to the new forest and keep the User Mailboxes still in the source forest (existing forest). As you all know, we have to create linked mailboxes in this scenario and the process would be,

Note – The source forest has Exchange 2007 Servers Org.

  1. Disabled the user account in source forest (Use ADUC)
  2. Disable the user mailbox(Disable-Mailbox cmdlet)
  3. Run the Clean-MailboxDatabase against the DB to quickly show the diconnected mailboxes in EMC
  4. Connect the mailbox as Linked Mailbox.

Imagine you have many users to be migrated, definitely the activity going to eat up a lot of your time. Use the simple piece of script to finish your linked mailbox creation in few minutes ….

Steps:
Imp Note - Ensure that the AD user accounts corresponding to the user mailbox (source AD accounts) are disabled during the account migration using ADMT. If you did not choose to disable the source account during account migration, you need to disable those account before going for linked mailbox creation.

1. Extract the mailbox report from exchange server using below command,

Get-Mailbox -Identity testif* -ResultSize 3000 |Select SamAccountName,UserPrincipalName,ExchangeGuid,Database | export-csv C:\userlist.csv

Imp: I have queries only for the user identity starts with "testif", you may put own filteration according to the need. The ResultSize is given as 3000 to get all the users, because of the default limit is 1000. If you have more than 3000 do modify the command accordingly.

This will give you a report in following format,

#TYPE System.Management.Automation.PSCustomObject
SamAccountName,UserPrincipalName,ExchangeGuid,Database
TestIf1,This email address is being protected from spambots. You need JavaScript enabled to view it.,9a124cdb-0b73-4568-957c-3c40c092be43,"CONTOSO-EX2K7\SG01\SG01-MB1"
TestIf2,This email address is being protected from spambots. You need JavaScript enabled to view it.,61363ac5-f16b-49d7-809b-167d730e62aa,"CONTOSO-EX2K7\SG01\SG01-MB1"

Remove the first line “#TYPE System.Management.Automation.PSCustomObject” and save it as it is. Keep in mind that this list will be used as the input user list for the script; hence only keep the user entries which are planned to migrate now. For e.g. you will have total of 2000 user and you have planned only for 100 now, keep only 100 entries and remove all 1900 entries from it(final list will be like below).

SamAccountName,UserPrincipalName,ExchangeGuid,Database
TestIf1,This email address is being protected from spambots. You need JavaScript enabled to view it.,9a124cdb-0b73-4568-957c-3c40c092be43,"CONTOSO-EX2K7\SG01\SG01-MB1"
TestIf2,This email address is being protected from spambots. You need JavaScript enabled to view it.,61363ac5-f16b-49d7-809b-167d730e62aa,"CONTOSO-EX2K7\SG01\SG01-MB1"

Now we are ready to move on to next phase(the above list can be prepaired well before the activity, so keep this ready before your migration schedule).

2. Disable the mailbox by importing the list created in the step1.
Import-CSV "C:\userlist.csv" |foreach{Disable-Mailbox -Identity $_.UserPrincipalName}

3. Run Clean-MailboxDatabase cmdlet if the mailboxes are not listed under the Disconnected Mailbox list of EMC.
If you are able to view all the listed users under the disconnected mailbox list, proceed to the next i.e. the final step.

4. Copy the below code and save it as .ps1 file under any folder(C:\E2K7-Scripts), and to execute the script you need to run "Set-ExecutionPolicy RemoteSigned"

# Migrate your User Mailbox to Linked mailbox for Cross forest user account migration, read tips below.
# _Disable mailbox prior using creation of linked mailbox scrip.
# _Created by – Pravee Balan(ExchangeDictionary.com)
# _Ensure you run "Set-ExecutionPolicy RemoteSigned" before running this script
# _Adding Exchange Snap In to execute Exchange CmdLets in this script
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
#Add required static value to the variables

$InputFile = "C:\userlist.csv" #User list file "userlist.csv" saved under the folder "C:\UserList"

#=====================================================================
#Creating the linked mailbox from the disconnected user mailboxes
#=====================================================================

Write-Host "Enter the Target Migration Admin Credentials"
Write-Host "Press any key when ready ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$InputFile = "C:\userlist.csv" #User list file "userlist.csv" saved under the drive "C:\"
$TargetADServer = "targetDC.fabrikam.com" #Target Forest Domain Controller
$TargetCred = Get-Credential #Enter the Target Forest Migration Crendentials
Import-CSV $InputFile|foreach{
$GUID=$_.ExchangeGuid
$DisconnectX = Get-MailboxStatistics | where{$_.disconnectdate -ne $null -and $_.MailboxGuid -eq $GUID}

if($DisconnectX -ne $null){
"Executing for  " + $_.UserPrincipalName
$targetuser=$_.SamAccountName+"@fabrikam.com" # Assuming that the target SamAccountName is same as source, if not please do required filteration.
Connect-Mailbox -Identity $_.ExchangeGuid -User $_.UserPrincipalName -Database $_.Database -LinkedDomainController $TargetADServer -LinkedMasterAccount $targetuser -LinkedCredential $TargetCred
}

if($DisconnectX -eq $null){$_.UserPrincipalName +"  disconnected mailbox not found !!!"}
}

Write-Host "Press any Key to Exit ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
#End of Script

Note – The script is saves as _CreateLinkedMailbox.ps1 in C:\E2K7-Scripts and ensure that you have modified the details marked in differrent colors in the above script.

5. Open the Exchange Management Shell and navigate to C:\E2K7-Scripts and run below,

 [PS] C:\E2K7-Scripts>.\_CreateLinkedMailbox.ps1 >>c:\output.txt

During the script execution it will ask you to enter the target domain admin credentials. I also have shown how to re-direct the output to different file " >>c:\output.txt"(keep in mind that the redirection works only for couple of formatted output and not all).
Open the file output to see the results, the output will be like below. If any one of the account failed to finish that will be shown as not found.

Executing for  This email address is being protected from spambots. You need JavaScript enabled to view it.
Executing for  This email address is being protected from spambots. You need JavaScript enabled to view it.

If you have see the output like "This email address is being protected from spambots. You need JavaScript enabled to view it. disconnected mailbox not found !!!" in the output file, that means the linked mailbox creation failed for those objects. Verify your steps and ensure those failed objects are appearing in the Disconnected mailbox list in EMC.

Write your comments and issues, it may need some changes to fit your infrastructure which are marked in different colors. I strongly recommend you to test this in a test environment before trying into your production servers.

 -Praveen

theme by reviewshub