<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AD Archives - IT Blog Site</title>
	<atom:link href="https://www.lavatelli.it/category/ad/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lavatelli.it/category/ad/</link>
	<description>Lavatelli IT Blog Site</description>
	<lastBuildDate>Thu, 30 Sep 2021 14:22:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>

<image>
	<url>https://www.lavatelli.it/wp-content/uploads/2019/02/ice_dragon-150x145.gif</url>
	<title>AD Archives - IT Blog Site</title>
	<link>https://www.lavatelli.it/category/ad/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Powershell script to import LDAP object into exchange contact</title>
		<link>https://www.lavatelli.it/powershell-script-to-import-ldap-object-into-exchange-contact/</link>
		
		<dc:creator><![CDATA[Franz]]></dc:creator>
		<pubDate>Fri, 01 Feb 2019 16:55:35 +0000</pubDate>
				<category><![CDATA[AD]]></category>
		<category><![CDATA[Exchange]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Windows]]></category>
		<guid isPermaLink="false">https://www.lavatelli.it/?p=463</guid>

					<description><![CDATA[<p>Whith this powershell script, it&#8217;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.</p>
<p>The post <a href="https://www.lavatelli.it/powershell-script-to-import-ldap-object-into-exchange-contact/">Powershell script to import LDAP object into exchange contact</a> appeared first on <a href="https://www.lavatelli.it">IT Blog Site</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="entry">


<p class="wp-block-paragraph">Whith this powershell script, it&#8217;s possible to import external ldap object into active directory mail-contact.</p>



<p class="wp-block-paragraph">Exchange will parse and create a list of contact available for everyone.</p>



<p class="wp-block-paragraph">The script will clear all OU before importing.<br></p>



<pre class="wp-block-code"><code>$ErrorActionPreference = 'Stop'
#load Exchange pssnapin
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.SnapIn
#load Assembly DirectoryServices
&#91;System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols") 
&#91;System.Reflection.Assembly]::LoadWithPartialName("System.Net") 
$body = ""
start-transcript
get-date
#---------------------------------------------
$OUS="TEST","TEST2","TEST3"
foreach ($OU in $OUS)
{
	#----------------------------------------------------------------------------------------------------------
	#load user and password to logon in Openldap
	$count = 0
	$UserName = "uid=reader,ou=users,dc=test,dc=eu"  
	$Password = "CgtCat34"
	$BODY= $body + "`n Processing OU "+$OU+"`n"
	$filter = "(objectclass=inetOrgPerson)"
	#Insert openLDAP source server and the OU of the company created in this openLDAP 
	$domain = "LDAP://10.241.37.1:389/o="+$OU+",dc=test,dc=eu"

	#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()
	#write-host "NO ERROR"
	#exit(1)
	get-mailcontact -ResultSize Unlimited -OrganizationalUnit ("OU="+$OU+",OU=LDAP-XXX,DC=TEST,DC=local") -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

	#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
			if ($user.properties.mail -eq $null)
			{ 
				$smtpmail=""
				write-host "the contact has empty email address" $user.properties.displayname
			}
			else
			{
				$smtpmail=""
				$smtpmail = &#91;Microsoft.Exchange.Data.ProxyAddress]("$($user.properties.mail)")		
			}

		if(-not(&#91;string]::IsNullOrEmpty($smtpmail.SmtpAddress))) # check if the smtp field is not empty
		{
		
		$mail = $smtpmail.SmtpAddress        
		write-host $user.properties.cn
	   
		If (&#91;string]$user.properties.displayname -ne (Get-MailContact (&#91;string]$user.properties.displayname) -ErrorAction silentlycontinue)) #check if the user exist in the AD yet   
		{
			If ( (get-mailcontact -ResultSize Unlimited  -OrganizationalUnit ("OU="+$OU+",OU=LDAP-XXX,DC=TEST,DC=local") -filter {WindowsEmailAddress -eq $mail}) -eq $null ) 
			{
				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-XXX,DC=TEST,DC=local") -ExternalEmailAddress $mail #-Alias $_.mailNickname
				Start-Sleep -s 30 #delay of 15 seconds to let AD to replicate the contact in the DCS servers		
				Write-host "update contacts properties.... " $user.properties.displayname
				Set-Contact -identity (&#91;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 (&#91;string]$user.properties.displayname) -CustomAttribute10 $OU
				Set-Mailcontact -identity (&#91;string]$user.properties.displayname) -CustomAttribute11 "updated"
				$BODY=$BODY+"ADDED "+$user.properties.displayname+"`n"
			}
			else
			{
				write-host "contact has been renamed"
			}
		}
		Else
		{
			write-host "the contact exist"
			Write-host "update contacts properties.... " $user.properties.displayname
			Set-Contact -identity (&#91;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 (&#91;string]$user.properties.displayname) -CustomAttribute10 $OU -ForceUpgrade
			Set-Mailcontact -identity (&#91;string]$user.properties.displayname) -CustomAttribute11 "updated" -ForceUpgrade
			#$BODY=$BODY+"UPDATED "+$user.properties.displayname+"`n"
		}
		
		
		}
		$count++
		}
	}
	#Remove all contact not updated
	Start-Sleep -s 60 #delay of 60 seconds to let AD to replicate the contact in the DCS servers
	Write-host "start removing removed contact.... "
	$removed=get-mailcontact -ResultSize Unlimited  -OrganizationalUnit ("OU="+$OU+",OU=LDAP-XXX,DC=TEST,DC=local") -filter {CustomAttribute11 -eq $null}|% { $_.Name}
	if ($removed) {
		$body = $body + "REMOVED " + ($removed -join "`nREMOVED ")
		$body = $body + "`n------------------------------------------------------`n"
	}
	get-mailcontact -OrganizationalUnit ("OU="+$OU+",OU=LDAP-XXX,DC=TEST,DC=local") -filter {CustomAttribute11 -eq $null}|remove-mailcontact -Confirm:$false
}
#----------------------------------------------------------------------------------------------------------
get-date
stop-transcript
$log = Get-childitem -Filter *.txt c:\users\administrator\Documents | sort LastWriteTime | select -last 1
send-mailmessage -from "Administrator &lt;administrator@test.it>" -to "Test &lt;dest@email.com>" -subject "LDAP Import" -attachment C:\users\administrator\Documents\$log -body $BODY -smtpServer smtp.isp.it
#----------------------------------------------------------------------------------------------------------
$Daysback = "-7"
 
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem -Filter *.txt c:\users\administrator\Documents | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
</code></pre>


</div><p><a class="a2a_button_email" href="https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fwww.lavatelli.it%2Fpowershell-script-to-import-ldap-object-into-exchange-contact%2F&amp;linkname=Powershell%20script%20to%20import%20LDAP%20object%20into%20exchange%20contact" title="Email" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.lavatelli.it%2Fpowershell-script-to-import-ldap-object-into-exchange-contact%2F&amp;linkname=Powershell%20script%20to%20import%20LDAP%20object%20into%20exchange%20contact" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.lavatelli.it%2Fpowershell-script-to-import-ldap-object-into-exchange-contact%2F&amp;linkname=Powershell%20script%20to%20import%20LDAP%20object%20into%20exchange%20contact" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fwww.lavatelli.it%2Fpowershell-script-to-import-ldap-object-into-exchange-contact%2F&amp;linkname=Powershell%20script%20to%20import%20LDAP%20object%20into%20exchange%20contact" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.lavatelli.it%2Fpowershell-script-to-import-ldap-object-into-exchange-contact%2F&amp;linkname=Powershell%20script%20to%20import%20LDAP%20object%20into%20exchange%20contact" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.lavatelli.it%2Fpowershell-script-to-import-ldap-object-into-exchange-contact%2F&#038;title=Powershell%20script%20to%20import%20LDAP%20object%20into%20exchange%20contact" data-a2a-url="https://www.lavatelli.it/powershell-script-to-import-ldap-object-into-exchange-contact/" data-a2a-title="Powershell script to import LDAP object into exchange contact"></a></p><p>The post <a href="https://www.lavatelli.it/powershell-script-to-import-ldap-object-into-exchange-contact/">Powershell script to import LDAP object into exchange contact</a> appeared first on <a href="https://www.lavatelli.it">IT Blog Site</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Object Caching 59/52 objects using Redis
Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Minified using Redis

Served from: www.lavatelli.it @ 2026-11-21 08:57:58 by W3 Total Cache
-->