M365 Cross Tenant Migration | Part IV

Tenant Migration
Reading Time: 3 minutes

With the migration of User, Shared and Resource mailboxes, is the biggest job done. When every mailbox that needs to be migrated, is set in a batch and waiting for a finalize, we can move on to migrate the rest like Distribution Groups and MailContacts. Mailcontacts and Distribution groups are not supported by Microsoft to migrate. Therefore you need to recreate them and populate them with the original members.

Distribution Groups

Distribution groups also need to have also the correct information set, otherwise users using the autofill option won’t be able to send email to that group. Also you don’t want to recreate those groups with all those settings and owners in the gui.

First connect to the SOURCE tenant: GetToTheCloudSource.onmicrosoft.com

#Exchange Online Management v3 Powershell module
Install-Module -Name ExchangeOnlineManagement -RequiredVersion 3.0.0 -Force

#Connect to Exchange Online to source tenant
Connect-ExchangeOnline

Inventory all the current groups with their properties

$DistributionGroups = Get-DistributionGroup -Resultsize Unlimited
$groupInfo = @()
ForEach ($Group in $DistributionGroups) {
    #$Members = (Get-DistributiongroupMember $Group.PrimarySMTPAddress).PrimarySMTPAddress
    $item = [PSCustomObject]@{
        Name                               = $Group.Name
        Alias                              = $Group.Alias
        DisplayName                        = $Group.DisplayName
        Description                        = $Group.Description
        legacyExchangeDN                   = $Group.legacyExchangeDN
        HiddenFromAddressListsEnabled      = $Group.HiddenFromAddressListsEnabled
        RequireSenderAuthenticationEnabled = $Group.RequireSenderAuthenticationEnabled
        ManagedBy                          = $Group.ManagedBy
        PrimarySmtpAddress                 = $Group.PrimarySmtpAddress
        EmailAddresses                     = $group.EmailAddresses -join ","
        ModeratedBy                        = $Group.ModeratedBy
        SendModerationNotifications        = $Group.SendModerationNotifications
        MemberDepartRestriction            = $Group.MemberDepartRestriction
        MemberJoinRestriction              = $Group.MemberJoinRestriction
        X500                               = $($Group.EmailAddresses) | Where-Object { $_ -like "X500:*" }
        CustomAttribute1                   = "CROSSTENANT"
        Members                            = (Get-DistributiongroupMember $Group.PrimarySMTPAddress).PrimarySMTPAddress
    }
    $GroupInfo += $item
}

In the same PowerShell window we now connect to the Target tenant: GetToTheCloudTarget.onmicrosoft.com

#Exchange Online Management v3 Powershell module
#Connect to Exchange Online to target tenant
Connect-ExchangeOnline

Creating the new distribution groups with the information in $groupInfo

# getting tenant id
$tenantName = (Get-OrganizationConfig).OrganizationalUnitRoot

# creating groups
$Notcreated = @()
ForEach ($Group in $groupInfo) {
    $LegacyExchangeDN = "X500:" + $Group.legacyExchangeDN
    if (!(Get-DistributionGroup $Group.Alias -ErrorAction SilentlyContinue)) {
        #creating new distribution group
        Try {
            New-DistributionGroup -Name $Group.Alias -DisplayName $Group.DisplayName -Alias $Group.Alias -Description $Group.Description -MemberDepartRestriction $Group.MemberDepartRestriction -MemberJoinRestriction $Group.MemberJoinRestriction -SendModerationNotifications $Group.SendModerationNotifications -ErrorAction STOP
            $group.NewGroup = $Group.Alias + "@" + $tenantName
            
        }
        catch {
            if ($($ERROR[0]).Exception.Message -Like "*Please Specify a unique value*") {
                Write-Output "ERROR: Groupname allready exists. Aborting creation..."
            }
            else {
                Write-Output $ERROR[0].Exception.Message
            }
        }
        do {
            $test = $null
            $test = Get-DistributionGroup $Group.Alias -ErrorAction SilentlyContinue
            Start-Sleep -seconds 2
        } while (
            $test -eq $null
        )

        Set-DistributionGroup $group.alias -CustomAttribute1 $Group.PrimarySmtpAddress
    }
    else {
        Write-Output "INFO: $($Group.DisplayName) allready exists. Checking configuration and members"
        $Notcreated += $group
    }

}

After creating adding the members related to the migration. Guest users who where member won’t be returning or recreated.

# getting all users which are involved cross tenant migration
$Mailboxes = Get-Mailbox | Select Identity, Alias, CustomAttribute1, CustomAttribute2 | Where-Object {$_.CustomAttribute1 -eq "CROSSTENANT"}
$Mailusers = Get-MailUser | Select Identity, Alias, CustomAttribute1, CustomAttribute2 | Where-Object {$_.CustomAttribute1 -eq "CROSSTENANT"}
$Combined = $Mailboxes
$Combined += $Mailusers

In a combined array there all the users that are migrated or to be migrated. We created the users before with the CustomAttribute1 = CROSSTENANT to use for filtering.

#adding members
ForEach ($Group in $GroupInfo) {
    
    If ($Group.Members -eq $null) {
        Write-Host "INFO: $($Group.DisplayName) has no members. Continue..."
    }
    else {
        ForEach ($Member in $Group.Members) {
            $user = $Combined | Where-object { $_.CustomAttribute2 -eq $member }
            if (!($user)) {
                #skip
            }
            else {
                Try {
                    Add-DistributionGroupMember $Group.NewGroup -Member $user.Identity -ErrorAction Stop
                    Write-Host "INFO: User $($User.Identity) was added to $($Group.DisplayName)"
                }
                Catch {
                    $Stop = $_
                    If ($Stop.Exception.Message -like "*is already a member of the group*") {
                        $message = "because is already member"
                    }
                    Write-Host "ERROR: Cannot add $($User.Identity) to $($Group.DisplayName) $message ..."

                }
            }
        }
    }
}

Now all the distribution lists are recreated with the original members added in the new TargetTenant.

Share and Enjoy !

Shares
WP Twitter Auto Publish Powered By : XYZScripts.com
We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active

Who we are

Our website address is: https://www.gettothe.cloud

Comments

When visitors leave comments on the site we collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection. An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your comment, your profile picture is visible to the public in the context of your comment.

Media

If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.

Cookies

If you leave a comment on our site you may opt-in to saving your name, email address and website in cookies. These are for your convenience so that you do not have to fill in your details again when you leave another comment. These cookies will last for one year. If you visit our login page, we will set a temporary cookie to determine if your browser accepts cookies. This cookie contains no personal data and is discarded when you close your browser. When you log in, we will also set up several cookies to save your login information and your screen display choices. Login cookies last for two days, and screen options cookies last for a year. If you select "Remember Me", your login will persist for two weeks. If you log out of your account, the login cookies will be removed. If you edit or publish an article, an additional cookie will be saved in your browser. This cookie includes no personal data and simply indicates the post ID of the article you just edited. It expires after 1 day.

Embedded content from other websites

Articles on this site may include embedded content (e.g. videos, images, articles, etc.). Embedded content from other websites behaves in the exact same way as if the visitor has visited the other website. These websites may collect data about you, use cookies, embed additional third-party tracking, and monitor your interaction with that embedded content, including tracking your interaction with the embedded content if you have an account and are logged in to that website.

Who we share your data with

If you request a password reset, your IP address will be included in the reset email.

How long we retain your data

If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue. For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.

What rights you have over your data

If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.

Where we send your data

Visitor comments may be checked through an automated spam detection service.
Save settings
Cookies settings