Browsed by
Category: Exchange

Powershell script to import LDAP object into exchange contact

Powershell script to import LDAP object into exchange contact

Whith this powershell script, it’s possible to import external ldap object into active directory mail-contact.

Exchange will parse and create a list of contact available for everyone.

The script will clear all OU before importing.

$count = 0
#load Exchange pssnapin
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010
#load Assembly DirectoryServices
[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols") 
[System.Reflection.Assembly]::LoadWithPartialName("System.Net") 
#load user and password to logon in Openldap
$UserName = "uid=reader,ou=users,dc=example,dc=com"  
$Password = "Password"
$OU = "OU-IMPORT"

$filter = "(objectclass=inetOrgPerson)"
#Insert openLDAP source server and the OU of the company created in this openLDAP 
$domain = "LDAP://10.10.10.1:389/o="+$OU+",dc=example,dc=com"

#Launch the search in the openLDAP
$root = New-Object -TypeName System.DirectoryServices.DirectoryEntry($domain,$UserName,$Password,'FastBind')
$query = New-Object System.DirectoryServices.DirectorySearcher($root,$filter)
$objuser = $query.findall()

#search user by user in the openLDAP ou
foreach ($user in $objUser.GetEnumerator()) {
  
    #this counter is only a security counter and for testing porpouses, in case of you dont want to launch all users at the same time
    if ($count -ge 0) #insert the number of users you want to import
    { 
    write-host "-------------------------------------------------------"
    #select the mail of the user in openLDAP
    $smtpmail = [Microsoft.Exchange.Data.ProxyAddress]("$($user.properties.mail)")		

if(-not([string]::IsNullOrEmpty($smtpmail.SmtpAddress))) # check if the smtp field is not empty
    {
    
    $mail = $smtpmail.SmtpAddress        
    write-host $user.properties.cn
   
  If ([string]$user.properties.displayname -ne (Get-MailContact ([string]$user.properties.displayname) -ErrorAction silentlycontinue)) #check if the user exist in the AD yet   
	{
	    write-host "the contact doesnt exist, I create it"
    	#change the OU where the contacts will be created in your AD, changing "-organizationalunit" property
        New-MailContact -Name $user.properties.cn -DisplayName $user.properties.displayname -FirstName $user.properties.givenname -LastName $user.properties.sn -OrganizationalUnit ("OU="+$OU+",OU=LDAP-Tesa,DC=CGTE,DC=local") -ExternalEmailAddress $mail #-Alias $_.mailNickname
		Set-Mailcontact -identity ([string]$user.properties.displayname) -CustomAttribute10 $OU
		Set-Mailcontact -identity ([string]$user.properties.displayname) -CustomAttribute11 "updated"
    }
	Else
	{
	    write-host "the contact exist, I wait for a 2 secons"
		#Start-Sleep -s 15 #delay of 5 seconds to let AD to replicate the contact in the DCS servers
		Write-host "update contacts properties.... " $user.properties.displayname
        Set-Contact -identity ([string]$user.properties.displayname) -Phone $user.properties.telephonenumber -mobilePhone $user.properties.mobile -Office $user.properties.physicaldeliveryofficename -Title $user.properties.title -Department $user.properties.department -Company $user.properties.o -city $user.properties.l
	    Set-Mailcontact -identity ([string]$user.properties.displayname) -CustomAttribute10 $OU
		Set-Mailcontact -identity ([string]$user.properties.displayname) -CustomAttribute11 "updated"
	}
    
    
    
    }
    $count++
 }
}
#Remove contact not update, aka deleted from ldap
get-mailcontact -OrganizationalUnit ("OU="+$OU+",DC=example,DC=com") -filter {CustomAttribute11 -eq $null}|remove-mailcontact -Confirm:$false
Start-Sleep -s 30 #delay of 30 seconds to let AD to replicate the contact in the DCS servers
get-mailcontact -OrganizationalUnit ("OU="+$OU+",DC=example,DC=com") -filter {CustomAttribute11 -ne $null}|set-mailcontact -CustomAttribute11 ""
Start-Sleep -s 30 #delay of 30 seconds to let AD to replicate the contact in the DCS servers