Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting and Importing Contacts to/from Exchange

Status
Not open for further replies.

Stevehewitt

IS-IT--Management
Jun 7, 2001
2,075
GB
Hi Guys,

Just been "aquired" by a new parent company, and whilst we seem to be staying on our own AD forest the business wants our address books to be syncronised and in the GAL of both businesses.

As such I needed a way to export all mail-enabled user accounts in AD into CSV, and then a way to import contacts into AD from CSV and make them mail-enabled.

Works in Exchange 2007 but should also work OK for Exchange 2010 environments too (though not tested)

This is a mix up from various sources on the internet, as well as my own changes and edits...

Exporting:

Code:
$Fields = "displayName,sn,givenName,mail,department," + `
  "title,telephoneNumber,Mobile"
$Fields = $Fields.Split(",")
 
$DomainRoot = New-Object System.DirectoryServices.DirectoryEntry
$Filter = "(&(objectClass=user)(objectCategory=person)(mail=*))"
 
$Searcher = New-Object System.DirectoryServices.DirectorySearcher($DomainRoot, $Filter)
$Fields | %{ $intPropertyNo = $Searcher.PropertiesToLoad.Add($_) }
 
$Results = @()
$Searcher.FindAll() | %{ 
  $Command = ""; $Fields | %{ $Command += [String]"@{n=`"$_`";e={`$_." + $_.ToLower() + "}}, ```n" }
  $Command = $Command.SubString(0, ($Command.Length - 4))
  $Command = "`$Results += `$_.Properties | Select-Object " + $Command
  Invoke-Expression $Command
}
 
$Results | Export-CSV -Path Export.csv

Note that the export dumps some random crap on line 1 of the CSV export that will need deleting before doing an import against it..!!

Importing:

Code:
Add-Content debug.txt "::Starting Import: ";

$err = "";

Import-Csv Export.csv | ForEach-Object{

New-MailContact -DomainController controller01.domain.local -Name $_."displayName" -Firstname $_."givenName" -Lastname $_."sn" -ExternalEmailAddress $_."mail" -OrganizationalUnit "domain.local/Contacts" -ErrorAction SilentlyContinue -ErrorVariable +err | Set-Contact -DomainController controller01.domain.local -Company "Random Company" -Phone $_."telephoneNumber" -Department $_."department" -Mobile $_."Mobile" -Title $_."title" -ErrorAction SilentlyContinue -ErrorVariable +err;

}
Add-Content debug.txt $err;

Note that on the import script you will need to put in your own Domain Controller (used twice in the script), and will want to change the OU it pumps the contacts out to. Also the "Company" field isn't imported but uses a static string so you'll want to change that too unless you want "Random Company" listed in all your contacts! :)

Hope this is useful to someone..!

Cheers,


Steve.

"They have the internet on computers now!" - Homer Simpson
 
Well, you could just configure ILM to do cross-forest GAL sync. That's what it's for.

Also, you don't need to specify a domain controller unless there is a logistical reason to do so. It works fine without it.

Pat Richard MVP
Plan for performance, and capacity takes care of itself. Plan for capacity, and suffer poor performance.
 
Doing a cross-forest GAL requires a connection to the remote AD forest.

If you don't want a site-to-site VPN then this script should allow periodic manual updates into AD for contacts.

Steve.

"They have the internet on computers now!" - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top