Set AD Email Address to PrimarySmtpAddress in matching Exchange Contact

Synopsis
I wrote this little script to address a issue where another admin/help desk/etc created a bunch of exchange contacts for external users and then later we ended up creating some AD accounts for some of those users so they could access some Linux/Unix systems we host. However we ran into a issue where those users had no way to be notified of password’s expire. So to address that I wrote this script Powershell Script to Notify Users of Expired or About to Expire Passwords in Active Directory which worked fine but then I found I couldn’t email some users because I didn’t have a email address for them. I did however have an exchange contact for some of them. So I wrote the follow script to copy their emails from the exchange contact into the AD Email attribute.

Requirements to run script:

Other Notes:
You will see some errors with this script if the Get-MailContact fails to find any info for the user in question. This is ok and is expected.

#Add Snapins
Add-PSSnapin "Quest.ActiveRoles.ADManagement" -ErrorAction SilentlyContinue

$users = Get-QADUser -SearchRoot 'OU=INTERNATIONAL OFFICE USERS,DC=domain,DC=net' -Enabled -PasswordNeverExpires:$false | where {($_.Email -eq $null)}

Foreach ($user in $users)
{
	$contact = Get-MailContact -Identity $user.Name
	if ($contact -ne $null)
	{
		Set-QADUser $user.Name -Email $contact.PrimarySmtpAddress
	}
	
}
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: