Created
December 16, 2021 19:46
-
-
Save JeremyTBradshaw/01c686b8219f5a1f6221dd627e07287a to your computer and use it in GitHub Desktop.
Capture LegacyExchangeDN's during Exchange org splits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
This is not intended to be an executable script. It's PS1 for now simply for syntax highlighting. | |
#> | |
#======#---------------------------# | |
#region# Backup LegacyExchangeDN's # | |
#======#---------------------------# | |
$LDNRecipients = @() | |
$Recipients = @() | |
$Recipients += Get-Mailbox -ResultSize Unlimited -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | |
$Recipients += Get-RemoteMailbox -ResultSize Unlimited -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | |
$Recipients += Get-DistributionGroup -ResultSize Unlimited -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | |
foreach ($rcpt in $Recipients) { | |
$_x500s = @("X500:$($rcpt.LegacyExchangeDN)") | |
$_x500s += $rcpt.EmailAddresses | Where-Object { $_ -like 'X500:*' } | |
$LDNRecipients += [PSCustomObject]@{ | |
RecipientTypeDetails = $rcpt.RecipientTypeDetails | |
DisplayName = $rcpt.DisplayName | |
PrimarySmtpAddress = $rcpt.PrimarySmtpAddress | |
X500Addresses = $_x500s -join '|||||' | |
} | |
} | |
$LDNRecipients | Export-Csv "$($HOME)\Desktop\$([datetime]::Today.ToString('yyyy-MM-dd'))_LegacyExchangeDNs_for_FWD-Contacts.csv" -NTI -Encoding UTF8 | |
#=========#---------------------------# | |
#endregion# Backup LegacyExchangeDN's # | |
#=========#---------------------------# | |
#======#----------------------# | |
#region# Create MailContact's # | |
#======#----------------------# | |
# Set a static DC to use for both the New-MailContact and follow-up Set-MailContact command: | |
$DomainController = "<DC FQDN>" | |
# Specify the OU wherein to create the MailContacts: | |
$TargetOU = "<domain.tld/OU1/OU2/OU3>" | |
$CreateContacts = Import-Csv "<Full/relative file path to CSV>" | |
foreach ($Contact in $CreateContacts) { | |
# Create the contacts: | |
try { | |
$newMailContactParams = @{ | |
Name = $Contact.DisplayName | |
ExternalEmailAddress = $Contact.PrimarySmtpAddress | |
OrganizationalUnit = $TargetOU | |
DomainController = $DomainController | |
ErrorAction = 'Stop' | |
} | |
New-MailContact @newMailContactParams | |
} | |
catch { | |
Write-Warning -Message "Failed to create MailContact for '$($Contact.DisplayName)' ($($Contact.PrimarySmtpAddress)). Error:`r`n$($_.Exception)" | |
} | |
# Add the LegacyExchangeDN's as X500's to the contacts: | |
try { | |
$setMailboxContactParams = @{ | |
Identity = $Contact.PrimarySmtpAddress | |
EmailAddresses = @{Add = @($Contact.X500Addresses -split '\|\|\|\|\|') } | |
HiddenFromAddressListsEnabled = $true | |
DomainController = $DomainController | |
ErrorAction = 'Stop' | |
} | |
Set-MailContact @setMailboxContactParams | |
} | |
catch { | |
Write-Warning -Message "Failed to add X500 addresses to '$($Contact.DisplayName)' ($($Contact.PrimarySmtpAddress)). Error:`r`n$($_.Exception)" | |
} | |
} | |
#=========#----------------------# | |
#endregion# Create MailContact's # | |
#=========#----------------------# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment