Stevehewitt
IS-IT--Management
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:
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:
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
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