Jump to content


  • 0
Andersson

Migration Preparation script

Question

This script idea came up after being involved in a migration project.

Published: 2013-05-22
Updated: 2013-05-24
Version: 1.1

It’s purpose with this script is for getting Quest Migration Manager (QMM) being able to match objects between domains. In a scenario with one-way trust and not being able to use Quest Migration Manager AD or any other tool for providing the SIDHistory into the AD objects. Users, mailboxes and mail contacts are already created, where the contacts are used for having a global address list (GAL) object they can send emails to.
Using QMM you can match by Username, Mailaddress or SIDHistory. In my case the username differs and the SIDHistory is not available (not allowed being copied into the target AD).

I created four different functions within this script, one called “Export-SourceInformation”, which should be used in the source environment. It will export the information from the source regarding Name, DisplayName, PrimarySmtpAddress, RecipientTypeDetails and save it into a CSV file called “users.csv”.
Then bring the CSV file into the target environment. This CSV file should be used as a control file if you don’t want to run all users at the same time (run some tests before running all of them in one batch).
I would recommend a couple of smaller batches for testing the functions before deploying it in full scale.
There is a function called “Verify-TargetInformation”, which uses the CSV file called “users.csv” and retrieves the Name and PrimarySmtpAddress for each object in the CSV file.

Another function is called “Set-SourceAddress”, this part takes care of the target account.
It uses the file called “users.csv” and checks whether there are any contacts for these mailboxes, if there are the mail contact will be deleted and the mailbox forward settings will be removed together with making sure that the mailbox is showed in the GAL. But before any changes are done, the current configuration for both the mail contact and the mailbox object are being saved into a CSV file called “targetinformation.csv”.

Last but not least, the function called “Rollback-TargetInformation” is used for putting back the PrimarySmtpAddress to the value that it was prior to the change, this by using the CSV file “targetinformation.csv”. When the Directory Synchronization have successfully matched the mailboxes this function should be runned for having back the correct information.

You can use this for free, without any guarantee or warranty and at your own risk.
Feel free to post about it, just make sure to link my blog and blogpost.

Download the script

  1. #####################################################################################
  2. # Filename: Migration-Preparation-testlabs.ps1
  3. # Description:
  4. # This PowerShell script exports information, configures objects and prepares for
  5. # Quest Migration Manager EX Directory Synchronization
  6. #
  7. # Usage: Import-Module Migration-Preparation-testlabs.ps1
  8. # Start with importing the module, then Starting function: Export-SourceInformation;
  9. # Set-SourceAddress; Rollback-TargetInformation; Verify-TargetInformation
  10. #
  11. # Version: 1.1
  12. #
  13. # Changelog:
  14. # v1.1 - Introduced LegacyExchangeDN - X500 migration
  15. #
  16. # Jonas Andersson, MCC 2011 & 2012
  17. # http://www.testlabs.se/blog
  18. # Twitter @jonand82
  19. #####################################################################################
  20.  
  21. Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue
  22. Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue
  23.  
  24. ## Exporting the SMTP information from Source mailboxes
  25. Function Export-SourceInformation(){
  26. $filename = "users.csv"
  27. $users = Get-Mailbox -resultsize unlimited | Select Name,DisplayName,PrimarySmtpAddress,RecipientTypeDetails,LegacyExchangeDN
  28. $users | Export-CSV $filename -notype -Encoding Unicode
  29. }
  30.  
  31.  
  32. ## Changing Target mailboxes to Source PrimarySmtpAddress for QMM to match on it
  33. Function Set-SourceAddress(){
  34. $data = Import-CSV .\users.csv
  35. $filename = "targetinformation.csv"
  36. ## Saving Target Information into CSV file before changing
  37. $MasterList = @()
  38. Foreach($t in $data)
  39. {
  40. $str = $t.displayname
  41. $MyObject = New-Object PSObject -Property @{
  42. Name = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").Name
  43. DisplayName = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").DisplayName
  44. PrimarySmtpAddress = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").PrimarySmtpAddress
  45. RecipientTypeDetails = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").RecipientTypeDetails
  46. Email = ((Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").EmailAddresses -Join ";")
  47. DeliverToMailboxAndForward = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").DeliverToMailboxAndForward
  48. ForwardingAddress = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").ForwardingAddress
  49. ForwardingSmtpAddress = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").ForwardingSmtpAddress
  50. HiddenFromAddressListsEnabled = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").HiddenFromAddressListsEnabled
  51. LegacyExchangeDN = (Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'").LegacyExchangeDN
  52. ContactName = (Get-MailContact -resultsize unlimited -Filter "DisplayName -like '*$str*'").Name
  53. ContactDisplayName = (Get-MailContact -resultsize unlimited -Filter "DisplayName -like '*$str*'").DisplayName
  54. ContactPrimarySmtpAddress = (Get-MailContact -resultsize unlimited -Filter "DisplayName -like '*$str*'").PrimarySmtpAddress
  55. ContactEmail = ((Get-MailContact -resultsize unlimited -Filter "DisplayName -like '*$str*'").EmailAddresses -Join ";")
  56. ContactExternalEmailAddress = (Get-MailContact -resultsize unlimited -Filter "DisplayName -like '*$str*'").ExternalEmailAddress
  57. ContactHiddenFromAddressListsEnabled = (Get-MailContact -resultsize unlimited -Filter "DisplayName -like '*$str*'").HiddenFromAddressListsEnabled
  58.  
  59. }
  60. $MasterList += $MyObject
  61. }
  62. $MasterList | Export-Csv $filename -NoTypeInformation -Encoding Unicode
  63. Write-Host "Information is saved into the CSV file: $filename" -ForegroundColor White
  64. Foreach($i in $data)
  65. {
  66. $str = $i.displayname
  67. ## Removing mail contacts for Source users
  68. $c = Get-MailContact -resultsize unlimited -Filter "DisplayName -like '*$str*'"
  69. Write-Host $c -ForegroundColor Yellow
  70. ## Multiple matching, no changes will be done
  71. if ($c.count -ge 1)
  72. {
  73. Write-Host "ERROR: Multiple matching"
  74. Write-Host "Matches: $c.count"
  75. return
  76. }
  77. ## Unique contact found, removing it
  78. if ($c.count -eq $null)
  79. {
  80. Write-Host "Unique matching: $c"
  81. Write-Host "Removing the mail contact object for: $c"
  82. Remove-MailContact -Identity $c -Confirm:$false
  83. }
  84. ## Starting the configuration for Target Mailboxes
  85. $u = Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'"
  86. Write-Host $u -ForegroundColor Yellow
  87. ## Multiple matching, no changes made
  88. if ($u.count -ge 1)
  89. {
  90. Write-Host "ERROR: Multiple matching"
  91. Write-Host "Matches: $u.count"
  92. return
  93. }
  94. ## Setting the Source PrimarySmtpAddress on the Target Mailboxes
  95. if ($u.count -eq $null)
  96. {
  97. Write-Host "Unique matching: $u"
  98. Write-Host "Setting Source SMTP as PrimaryAddress for matching using QMM"
  99. Write-Host "Configuring the mailbox for showing up in GAL and remove the forwarding configuration"
  100. Write-Host "Adding the LegacyExchangeDN as X500 for Outlook auto-complete cache"
  101. $email = $i.PrimarySmtpAddress
  102. $x500 = $i.LegacyExchangeDN
  103. Set-Mailbox -Identity $u -ForwardingAddress $null -DeliverToMailboxAndForward:$false -HiddenFromAddressListsEnabled:$false -PrimarySmtpAddress $email -EmailAddressPolicyEnabled:$false
  104. $ProxyAddresses = (Get-Mailbox -Identity $u).EmailAddresses
  105. $ProxyAddresses += [Microsoft.Exchange.Data.CustomProxyAddress]("X500:$x500")
  106. Set-Mailbox -Identity $u -EmailAddresses $ProxyAddresses
  107. }
  108. else
  109. {
  110. Write-Host "No match"
  111. return
  112. }
  113. }
  114. Write-Host ""
  115. Write-Host "#################################################################" -ForegroundColor White
  116. Write-Host "# First run the Verify-TargetInformation.. #" -ForegroundColor White
  117. Write-Host "# Start the QMM Synchronization for matching Source <-> Target #" -ForegroundColor White
  118. Write-Host "# Finally run the Rollback-TargetInformation when sync is done #" -ForegroundColor White
  119. Write-Host "#################################################################" -ForegroundColor White
  120. Write-Host ""
  121. }
  122.  
  123.  
  124. ## Rollback of Target PrimarySmtpAddress
  125. Function Rollback-TargetInformation(){
  126. $data = Import-CSV .\targetinformation.csv
  127. $MasterList = @()
  128. Foreach($i in $data)
  129. {
  130. $str = $i.displayname
  131. $user = Get-Mailbox -resultsize unlimited -Filter "DisplayName -like '*$str*'"
  132. ## Predicts there is only one @testlabs.se address
  133. $pri = Get-Mailbox -Identity $user | Select-Object -ExpandProperty EmailAddresses | Where-Object {$_.SmtpAddress -like '*@testlabs.se'} | Select-Object SmtpAddress
  134. $adr = $pri.SmtpAddress
  135. Write-Host "Configuring mailbox: $user with adress: $adr" -ForegroundColor White
  136. Set-Mailbox -Identity $user -PrimarySmtpAddress $adr -EmailAddressPolicyEnabled:$False
  137. }
  138. }
  139.  
  140.  
  141. ## Verifying the Target PrimarySmtpAddress
  142. Function Verify-TargetInformation(){
  143.  
  144. $data = Import-CSV .\users.csv
  145. $filetime = (get-date -format yyyyMMdd-hhmm)
  146. $filename = "verify-$filetime.csv"
  147. $MasterList = @()
  148. ## Verifying the PrimarySmtpAddress
  149. Foreach($i in $data)
  150. {
  151. $MyObject = New-Object PSObject -Property @{
  152. Name = (Get-Mailbox -Identity $i.PrimarySmtpAddress).Name
  153. PrimarySmtpAddress = (Get-Mailbox -Identity $i.PrimarySmtpAddress).PrimarySmtpAddress
  154. Database = (Get-Mailbox -Identity $i.PrimarySmtpAddress).Database
  155. EmailAddresses = ((Get-Mailbox -Identity $i.PrimarySmtpAddress).EmailAddresses -Join ";")
  156. }
  157. $MasterList += $MyObject
  158. }
  159. $MasterList | Export-Csv $filename -NoTypeInformation -Encoding Unicode
  160. Import-CSV $filename
  161. Write-Host ""
  162. Write-Host "SMTP Verification is saved into the CSV file: $filename" -ForegroundColor White
  163. Write-Host ""
  164. }
  165.  

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.