Add-PSSnapin Quest.ActiveRoles.ADManagement
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
clear
$Error.Clear()
#############################################################
# Changes need to be made to all of the following #
#############################################################
#MY Domain details.
$TargetForest="yourdomainname.com"
$TargetOU = "OU=External Contacts,DC=yourdomainname,DC=com" # Where contacts will go
$TargetDC = "MyDc.yourdomainname.com" # Pref is for GC PDC
$TargetContactSuffix = " (The other domain)" # Appended to name i.e. "AUser Name (The other Domain name)"
# SOURCE - Where to read remote domains users from
$SourceForest="theotherdomain.com" # Domain name of Source of Contacts
$SourceDC = "DC1.theotherdomain.com" # Name of any DC in source Domain
$SourceOU = "OU=Myusers,DC=theotherdomain,DC=com" # Base OU/container to read users from
#############################################################
# end of config section #
#############################################################
#Connect to Source forest AD
Connect-QADService -Service $SourceForest
$SourceForestUsersToMigrate = Get-QADUser -SearchRoot $SourceOU -SearchScope Subtree -LdapFilter "(homeMDB=*)" -SizeLimit 0
#Connect to Destination Forest AD
Connect-QADService -Service $TargetForest
$UserCount =0
$NewUsers = @()
$MailErrors = @()
$SourceForestUsersToMigrate | ForEach-Object {
New-MailContact -DomainController $TargetDC -Name ($_.DisplayName + $TargetContactSuffix) -DisplayName ($_.DisplayName + $TargetContactSuffix) -FirstName $_.FirstName -LastName $_.LastName -Alias ($_.FirstName + ($_.LastName).ToUpper()) -OrganizationalUnit $TargetOU -ExternalEmailAddress $_.Email -ErrorAction SilentlyContinue
if ( $? -ne $true ) { #new-mailcontact failed if $False
# Why did the creation fail?
if ( $Error[0].exception.Gettype().fullname -eq "Microsoft.Exchange.Configuration.ObjectModel.ProxyAddressExistsException" ) {
"Email address already Exists: " + $_.email
$Contact = Get-Contact $_.UserPrincipalName
If ($? -ne $true ) {
$MailErrors += ("Could not do " + $_.userprincipalname + ":" + $_.email + "`n")
} else {
Set-Contact $Contact -Phone $_.PhoneNumber -Office $_.office -Title $_.title -Company $_.company -Department $_.department
}
}
}
else {
"Email Conatact added: " + ($_.DisplayName + $TargetContactSuffix) + " : " + $_.email
set-mailcontact -DomainController $TargetDC -Identity ($_.DisplayName + $TargetContactSuffix) -EmailAddressPolicyEnabled $false
Set-Mailcontact -DomainController $TargetDC -Identity ($_.DisplayName + $TargetContactSuffix) -EmailAddresses $_.Email
$NewUsers += ( $_.Email + " `n")
$UserCount = $UserCount + 1
}
}
$NewUsers
$MailErrors
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
This works fantastic, except for one thing. Is there a way to prevent the target forest from creating an SMTP address for the users in the target domain?
Example:
Bob@SourceForest.com email address gets created
Bob@TargetForest.com also gets created but set to secondary address
Or does it even matter? It doesn't appear that it has affected anything but I didn't know if that would cause any issues or not.
Also, in the future if I wish to use federation can I simply delete the contacts before federating?
Author
Commented:Deleting contacts in exchange is a nightmare!! Dont forget that outlook remembers the contact reference stored in exchange once you have used it once. If you delete a contact from exchange and then click reply on an email to that contact in outlook thet you had BEOFRE you deleted the contact and recreated it, it will fail with a contact not found.
Commented:
So if I understand you right, once I go to delete all of these contacts and setup federation (or in our case I think we'll be using GALSYNC from netsec, then at that point I will need to send out a global email telling people to stop replying to the old contact? The new contacts in the future wouldn't have the append info you have in your script, just their normal information.