How to Sync Exchange Online Proxy Addresses with Active Directory
Sync Exchange Online Proxy Addresses with Active Directory
Section titled “Sync Exchange Online Proxy Addresses with Active Directory”This script connects to Exchange Online, retrieves all mailboxes, and matches each mailbox with a corresponding Active Directory (AD) user based on the primary SMTP address. It then filters out email addresses from a specified excluded domain and formats the remaining proxy addresses. These filtered proxy addresses are then added to the AD user. The script also includes error handling for cases where no or multiple matching AD users are found, and logs any issues with updating the AD user.
Script Overview
Section titled “Script Overview”Ideal for: Exchange administrators managing hybrid email environments
Key Features:
- Mailbox Synchronization - Connects Exchange Online with Active Directory
- Domain Filtering - Excludes specified domains from proxy address sync
- Error Handling - Comprehensive logging for troubleshooting
- Batch Processing - Handles multiple mailboxes efficiently
Script Implementation
Section titled “Script Implementation”# Define the Exchange Online organization and excluded domain
$Organization = "xxx.com"$ExcludeDomain = "xxx.onmicrosoft.com"
# Connect to Exchange Online
Connect-ExchangeOnline -Organization $Organization -ErrorAction Stop
# Get all mailboxes
$AllMailboxs = Get-Mailbox
# Get all Active Directory users
$AllADUsers = Get-ADUser -Filter *
# Loop through each mailbox
foreach ($Mailbox in $AllMailboxs) { # Find the corresponding AD user based on the mailbox's primary SMTP address
$ADUser = $AllADUsers | Where-Object EmailAddress -eq $($Mailbox.PrimarySmtpAddress)
# Handle different scenarios based on the number of matching AD users
switch ($ADUser.count) { 0 { Write-Warning "No AD User found with $($Mailbox.PrimarySmtpAddress) E-mail address" } {$_ -gt 1} { Write-Warning "Multiple AD Users found with $($Mailbox.PrimarySmtpAddress) E-mail address" } 1 { # Filter out addresses with excluded domain and correct formatting
$Addresses = $Mailbox.EmailAddresses | Where-Object {($_ -like "smtp:*") -and ($_ -notlike "*$ExcludeDomain")}
if ($Addresses) { # Correct formatting for proxy addresses
$Addresses = $Addresses -creplace "SMTP:", "smtp:"
# Prepare hash to add proxy addresses
$HashToAdd = @{'proxyAddresses' = $Addresses}
try { # Update the AD user with new proxy addresses
$ADUser | Set-ADUser -Add $HashToAdd -ErrorAction Stop Write-Output "Updated $($Mailbox.EmailAddresses) proxyAddresses successfully" } catch { Write-Output "Failed to update $($Mailbox.EmailAddresses) proxyAddresses. Error: $($_.Exception.Message)" } } } }}Configuration Requirements
Section titled “Configuration Requirements”Prerequisites
Section titled “Prerequisites”| Component | Requirement | Purpose |
|---|---|---|
| Exchange Online Module | ExchangeOnlineManagement | Connect to Exchange Online |
| Active Directory Module | ActiveDirectory | Manage AD user attributes |
| Exchange Admin Rights | Mailbox management permissions | Access mailbox data |
| AD Admin Rights | User attribute modification | Update proxy addresses |
Configuration Parameters
Section titled “Configuration Parameters”| Parameter | Example | Description |
|---|---|---|
| Organization | "contoso.com" | Exchange Online organization |
| ExcludeDomain | "contoso.onmicrosoft.com" | Domain to exclude from sync |
Script Logic Flow
Section titled “Script Logic Flow”Processing Steps
Section titled “Processing Steps”-
Connect to Exchange Online
- Establish authenticated connection
- Retrieve all mailbox data
-
Retrieve Active Directory Users
- Get all AD users with email attributes
- Prepare for matching process
-
Mailbox-AD User Matching
- Match based on primary SMTP address
- Handle multiple match scenarios
-
Address Filtering and Formatting
- Exclude specified domains
- Format proxy addresses correctly
-
Update Active Directory
- Apply filtered proxy addresses
- Log success/failure status
Error Handling Scenarios
Section titled “Error Handling Scenarios”| Scenario | Response | Resolution |
|---|---|---|
| No AD User Found | Warning logged | Manual user creation required |
| Multiple AD Users | Warning logged | Review duplicate accounts |
| Update Failure | Error logged | Check permissions and data |
Conclusion
Section titled “Conclusion”Key Takeaway: This script provides automated synchronization between Exchange Online and Active Directory, ensuring consistent email address management while maintaining control over domain inclusion through configurable filtering.