If your company has more than one Exchange environment or you are in the process of migrating, you will inevitably discover that the email platform does not have a native method for synchronizing contact information between multiple installations. When administrators first run into this situation the first thoughts are something along the lines of, “That’s ridiculous!” followed by, “Now what?”.

Run a Web search for GAL Sync and you will find a plethora of commercial tools made to do the job. I have personal experience with a couple of them and for the most part they work well. The issue is sticker shock. If you administrate a large environment, some of these tools can exceed six figures by the time you’ve purchased everything that you need.

Before you plunk down a giant wad of cash, consider doing it yourself with a little PowerShell know how. The last article on my blog described how to use Exchange to securely transfer files between organizations. If you use that technique to send contact data in the form of a CSV, it can be easily imported into AD on other domains.

Setup the Exchange File Transfer technique as described in this blog post. Now we’ll need to adjust the sending script to send Exchange contact data that can be imported into AD.

For true synchronization, you’ll need to run the sending and receiving process on all of the AD / Exchange environments involved. However, in most of the cases I’ve personally run into, only the Headquarters environment needed to have contact data for all the locations and companies.

#Author: Kevin-Trent@Hotmail.com 2019
# Export-ExchangeContactData.ps1
#This script will scan your Exchange Mailbox users and capture each person's Displayname and PrimarySmtpAddress. 
#The data will be put into a csv and e-mailed as an atttachment. 
#The Companyname variable below is used to name the files we send. The SMTP server is the fqdn of an SMTP relay to send the emails from.
#This script should be scheduled to run on an Exchange server once a day.

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchangeserver.mydomain.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session

Import-Module ActiveDirectory

#Variables:
$companyname = "MyCompany"
$smtpserver = "MyMailServer"
$reportname = $companyname+"-"+(get-date -f MM-dd-yyyy)

#Retrieve and format data. Add Properties in select line as needed.
Get-Mailbox -IgnoreDefaultScope -ResultSize Unlimited | 
Select Displayname,PrimarySmtpAddress | 
Export-CSV c:\temp\$reportname".csv" -NoTypeInformation

#Email the report:
Send-MailMessage -SmtpServer $smtpserver -To mailbox@mydomain.com -From mailbox2@mydomain.com -Subject "Contacts from $companyname" -Body "Please see the attached file $senderdomain" -Attachments C:\Temp\$reportname.csv
Start-Sleep -Seconds 60
Remove-Item -Path c:\temp\$reportname".csv"

Now all that is needed is an import of the contact data into Active Directory. Again, a few lines of PowerShell code will do the job.

#Author: Kevin-Trent@hotmail.com
#Import-Contacts.ps1
#Imports CSV data as AD contacts into the OU that you specifiy.
#Add Properties to match the data you've exported from the other exchange server. 

Import-Module ActiveDirectory
$Path = \\Server\Share\FileName

Import-Csv $Path | foreach{New-ADObject -Type Contact -Name $_.DisplayName -OtherAttributes @{'displayName'=$_.DisplayName;'mail'=$_.PrimaryEmailAddress} -Path "OU=$ou1,OU=$ou2,DC=$dc1,DC=$dc2,DC=$dc3"}
Advertisements