In an Exchange Crossforest migration the distribution groups can be a very painful operation that would cause loss of time, lots of issues and continues headache if not solved within a timely manner.
The migration can be a long boring process that needs to be as accurate as possible to avoid any issue related to members in the group or/and Group’s Primary SMTP details.
While doing a Crossforest migration I came through through this headache and tried to seek a script that would satisfy my migration’s requirements but only thing I found is the exportPowershell made by Satheshwaran Manoharan.
Export Process:
The script exports all groups and their members from the source forest, but to import there’s no option and I had to write my own script.
To make use of this script first make sure you that you have migrated the Groups with ADMT in the recommended order otherwise the migration would be problematic.
- First: Universal Groups
- Second: Global Groups
- Third: Domain Local Groups
Once groups are migrated to the target forest you can check how they look like through Exchange management shell and whether they have members added or SMTP address set.
After I checked it apparently shows that group is empty and has no Primary SMTP address associated with it.
Import Process:
In order to add members during the migration since this is a Hybrid/Coexistence migration not cutover, It took time to migrate users and therefore I have to add non-migrated users in target forest as External Contacts to the Distribution Groups and add migrated users as Mailbox users.
Then after adding the users I have to setup Primary SMTP address for the groups according to the exported CSV file from the Source Forest.
To Import users, I had to setup a CSV file with the following format:
In this format, the Display name, Alias, RecipientType and PrimarySMTPAddress belong to the User object that’s included in the group meanwhile, The Dgroup is the Distribution group’s Alias and DGSMTP is the Group’s Primary SMTP address.
The following script imports groups members to their relative groups
#########################################################################################
# If user type is Usermailbox then it’ll be in Target forest as a Contact #
#########################################################################################
$Users = Import-Csv “C:\Groups\dgs.csv”
Foreach ($User in $Users){
$GroupAlias = $User.Dgroup
$GroupSMTP = $User.DGSMTP
Write-Host “$User.Alias” has been Added to the Group $User.Dgroup -ForegroundColor Green -BackgroundColor Black
if ($User.RecipientType -Match “UserMailbox”){
Add-DistributionGroupMember -Identity $GroupAlias -Member $User.PrimarySMTP -BypassSecurityGroupManagerCheck}}
Fixing Distribution Groups Primary SMTP Address:
Since distribution groups are mostly imported without Primary SMTP address through ADMT then we’ll have to also make sure that we fix this for our groups, but what if the destination forest has similar groups or the SMTP is used already ? In order to avoid any mistake when associating the Primary SMTP address I have created a script that would check distribution groups with null value in their primary SMTP Address and copy the SMTP address to these groups avoiding any overwrite or change of the destination Distribution groups.
#########################################################################################
# Setup groups with Primary SMTP Address
#########################################################################################
$Groups = Import-Csv “C:\Groups\Group_test.csv”
Foreach ($Group in $Groups){
$GroupAlias = $Group.dgroup
$GroupSMTP = $Group.DGSMTP
if ((Get-DistributionGroup $GroupAlias | %{$_.PrimarySmtpAddress}) -match “$GroupSMTP”) {
Write-Host Group $GroupAlias already has $GroupSMTP Setup as primary SMTP address -ForegroundColor Yellow -BackgroundColor Red}else{
Set-DistributionGroup -Identity $GroupAlias -PrimarySmtpAddress $GroupSMTP -EmailAddressPolicyEnabled $False
Write-Host Group $GroupAlias has $GroupSMTP Setup as primary SMTP -ForegroundColor Green -BackgroundColor Black }}
The script will check if the groups has primary SMTP matches the one in the CSV file, if it doesn’t it’ll setup the primary SMTP address for that group with green color like in the below screenshot
You can use this script with the same CSV file that you will use for adding members to the groups too , If groups SMTP exists already you’ll get the following error
Note:
Attached below, You can find the new version of the powershell and the CSV along with it.