Migrating Distro Groups from Exchange to Office 365, part 3

In step 2 of part 1, I created online versions of all my on-premises distribution groups:

2. Create groups in Exchange Online. I used the list I created in step 1 to create corresponding groups in Exchange Online, appending ” OL” to the name of each group to avoid conflicts. This is easy enough to do using a formula in Excel to create the PowerShell code…. [Excerpted from part 1.]

Then in step 3, I wrote a PowerShell script to transfer the properties of my on-premises groups to my online groups:

3. Configure groups in Exchange Online. I wrote another PowerShell script (Import_DistroGroup.ps1) to configure the new online distribution groups using the CSV files exported in step 1. [Excerpted from part 1.]

Import_DistroGoups.ps1 (see below) reads each of the files created by Export_DistroGroups.ps1 (see part 2) and applies the contents to the Exchange Online version of each group. Once the script is done with that file, it moves it to a subfolder called “Completed.”

Migrating distribution groups from an on-premise Microsoft Exchange organization to Office 365.

As before: Evaluate this code and your environment carefully before using it. No guarantees. No promises. Use at your own risk.

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Filename: Import_DistroGroup.ps1
# Version:     2016.03.15
# Purpose: Imports the managers, allowed senders, allowed sender 
# groups, member-of groups, group members, and group email addresses
# from a collection of csv files at the path specified by the InPath 
# variable. The files were created by the Export_DistroGroup script.
#
# Note: This script uses the Quest ActiveRoles PowerShell snapin for
# retrieving the groups that this group is a member of.
# 
# Usage:
# Import_DistroGroup "Group Name"
# "Group Name" must be the exact name of the group in the on-premises
# Exchange organization.
# 

Param(
    [string]$Group
)

# Specify the path of your input files. This should be the same as
# the OutPath from Export_DistroGroup.
$InPath = "C:\Temp\Groups\"

# If the Completed folder doesn't exist, create it.
$CompletePath = $InPath + "Completed\"
if (-not (Test-Path $CompletePath)) {
    New-Item $CompletePath -Type Directory
}

# These variables tell the script where to find the individual csv
# files.
$AcceptFromFile = $InPath + $Group + "-AcceptFrom.csv"
$AcceptFromDLFile = $InPath + $Group + "-AcceptFromDL.csv"
$ManagersFile = $InPath + $Group + "-Managers.csv"
$MembersFile = $InPath + $Group + "-Members.csv"
$MemberOfFile = $InPath + $Group + "-MemberOf.csv"
$SettingsFile = $InPath + $Group + "-Settings.csv"

# This variable holds the name of the group as it will be created
# in Exchange Online.
$OLGroup = $Group + " OL"

# Load the Quest AD snapin.
if ((get-pssnapin -name quest.activeroles.admanagement -EA 0) `
  -eq $null) {
    Write-Host "Loading Quest..."
    Add-PsSnapin quest.activeroles.admanagement
}

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# If there is a an AcceptFrom file, add the list of allowed senders to
# the online group and move the file to the Completed folder.
if (Test-Path $AcceptFromFile) {
    Write-Host "Adding acceptable senders..."
    $AcceptFrom = Import-CSV $AcceptFromFile
    $AcceptFrom | Foreach {Set-DistributionGroup $OLGroup `
        -AcceptMessagesOnlyFrom @{add=$_.UserPrincipalName}}
    Move-Item $AcceptFromFile -Destination $CompletePath
}

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# If there is a an AcceptFromDL file, add the list of allowed senders
# to the online group and move the file to the Completed folder.
if (Test-Path $AcceptFromDLFile) {
    Write-Host "Adding acceptable Group senders..."
    $AcceptFromDL = Import-CSV $AcceptFromDLFile
    $AcceptFromDL | Foreach {Set-DistributionGroup $OLGroup `
        -AcceptMessagesOnlyFromDLMembers @{add=$_.Name}}
    Move-Item $AcceptFromDLFile -Destination $CompletePath
}

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# If there is a Managers file, add the managers to the ManagedBy
# attribute and move the file to the Completed folder.
if (Test-Path $ManagersFile) {
    Write-Host "Adding group managers..."
    $Managers = Import-CSV $ManagersFile
    $Managers | Foreach {
        Set-DistributionGroup $OLGroup `
            -ManagedBy @{add=$_.UserPrincipalName}
    }
    Move-Item $ManagersFile -Destination $CompletePath
}

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# If there is a Members file, add the members to the online group and
# move the file to the Completed folder. If there is no Members file,
# consider whether you need to migrate this group at all.
if (Test-Path $MembersFile) {
    Write-Host "Adding group members..."
    $Members = Import-CSV $MembersFile
    $Members | Foreach {Add-DistributionGroupMember $OLGroup `
        -Member $_.UserPrincipalName}
    Move-Item $MembersFile -Destination $CompletePath
}

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# If there is a MembersOf file, add this online group to the listed 
# groups. Skip any groups that the online group is already a member of.
# Move the file to the Completed folder.
if (Test-Path $MemberOfFile) {
    Write-Host "Adding group to other groups..."
    $MemberOf = Import-CSV $MemberOfFile
    $CurrentMemberOf = (Get-QADMemberOf $OLGroup | Select GroupName)
    $MemberOf | Foreach {
        if ($CurrentMemberOf.GroupName -NotContains $_.GroupName) {
            $GroupName = $_.GroupName + " OL"
            Add-DistributionGroupMember $GroupName -Member $OLGroup
        }
    }
    Move-Item $MemberOfFile -Destination $CompletePath
}

# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# If there is a Settings file, apply the contents to the online group
# and move the file to the Completed folder. If you modified this
# section in Export_DistroGroup, be sure to mirror those modifications
# here.
if (Test-Path $SettingsFile) {
    Write-Host "Writing miscellaneous group settings..."
    $Settings = Import-CSV $SettingsFile
    $Settings | Foreach {
        if ($_.HiddenFromAddressListsEnabled -eq "true") {
            $Hide = $True
        }
        else {$Hide = $False}
        if ($_.RequireSenderAuthenticationEnabled -eq "true") {
            $ReqAuth = $True
        }
        else {$ReqAuth = $False}
        Set-DistributionGroup $OLGroup `
            -HiddenFromAddressListsEnabled $Hide `
            -RequireSenderAuthenticationEnabled $ReqAuth
    }
    Move-Item $SettingsFile -Destination $CompletePath
}

Note 1: “-EA 0” is an abbreviated form of “-ErrorAction SilentlyContinue”. I used it the abbreviation to make formatting the script for WordPress simpler.
Note 2: If you spot a mistake, please let me know. If this information is useful to you, let me know that too.

Part 1 – Overview of the process I used to migrate my Exchange distribution groups to Office 365.
Part 2 – Exporting your on-premises distro group settings to CSV files.
Part 3 – Importing your group settings from the CSV files to Exchange Online.
Part 4 – Renaming and addressing your online distro groups.
Part 5 – Rebuilding your Dynamic Distribution groups online.

Leave a Reply

Your email address will not be published. Required fields are marked *