Exchange PowerShell Scripting – legacyexchangedn task with Distribution List

18 Oct

Script in step 1 will create a dynamic distribution list for all contacts in a specific OU that do not have legacyexchangedn filled.

Script in step 2 will walk through all current contacts found in the dynamic distribution list and add a legacyEchangeDN corresponding to the variables you have set.

Step 1

$searchincontainer = “<domain/OU-Path>”
# Example: $searchincontainer = “g3dom.biz/g3contact”
$createinContainer = “<domain/OU-Path>”
# Example: $createinContainer = “g3dom.biz/Users”

new-DynamicDistributionGroup -Name “NoLegacyExchangeDN”  -RecipientFilter {((RecipientType -eq ‘MailContact’) -and (LegacyExchangeDN -eq $null))} -OrganizationalUnit $createinContainer -Alias “NoLegacyExchangeDN”  -RecipientContainer $searchincontainer

NETsecHelpRequiredNew.JPG

Step 2

$prefix = “<path>”
# example: $prefix = “/o=PPFA/ou=National/cn=Recipients/cn=”

$group = Get-DynamicDistributionGroup “<dngroup>”
# example: $group = Get-DynamicDistributionGroup “NoLegacyExchangeDN”

$members =””
$objContainer =”LDAP://” + $group.RecipientContainer.DistinguishedName
$objDomain = New-Object System.DirectoryServices.DirectoryEntry $objContainer
$strFilter = $group.LdapRecipientFilter

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 25000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = “Subtree”

$colProplist = “name”,”distinguishedname”,”cn”
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

  foreach ($Result in $colResults)
  {
 $objContact = $Result.Properties
 $pathstring = “LDAP://” + $objcontact.distinguishedname

 $deContact = New-Object System.DirectoryServices.DirectoryEntry $pathstring

 $lex = $prefix + $decontact.mailnickname

 write-host “Setting legacyExchangeDN=” $lex ” for contact: ” $decontact.name
 $decontact.legacyexchangedn = $lex
 $decontact.setinfo()
  }

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *