timsstuff

Sync-GAL.ps1

Nov 19th, 2025
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.     [Parameter(Mandatory=$true)][string]$RemoteDC,
  4.     [Parameter(Mandatory=$true)][string]$RemoteOU,
  5.     [Parameter(Mandatory=$true)][string]$LocalOU,
  6.     [Parameter(Mandatory=$false)][System.Management.Automation.PSCredential]$RemoteCred,
  7.     [Parameter(Mandatory=$false)][string]$AddressList,
  8.     [Parameter(Mandatory=$false)][int]$MaxDeletions = 64,
  9.     [Parameter(Mandatory=$false)][switch]$ScheduledTask = $false,
  10.     [Parameter(Mandatory=$false)][string]$TargetDomain
  11.     )
  12. If($ScheduledTask) { Start-Transcript -Path .\Sync-GAL-$AddressList.log -Append }
  13.  
  14. Import-Module ActiveDirectory
  15.  
  16. #Get active remote users
  17. Try {
  18.     if($null -ne $RemoteCred) {
  19.         $RUsers = Get-ADUser -Server $RemoteDC -Credential $RemoteCred -LDAPFilter "(&(objectClass=user)(mail=*)(userAccountControl:1.2.840.113556.1.4.803:=512)(!userAccountControl:1.2.840.113556.1.4.803:=2))" `
  20.         -SearchBase $RemoteOU -Properties company, department, displayName, givenName, mail, physicalDeliveryOfficeName, sn, telephoneNumber, title, pager, ipPhone, legacyExchangeDN
  21.  
  22.         $RGroups = Get-ADGroup -Server $RemoteDC -Credential $RemoteCred -LDAPFilter "(mail=*)" -Properties displayname, mail, department, company, telephoneNumber, legacyExchangeDN
  23.     }
  24.     else {
  25.         $RUsers = Get-ADUser -Server $RemoteDC -LDAPFilter "(&(objectClass=user)(mail=*)(userAccountControl:1.2.840.113556.1.4.803:=512)(!userAccountControl:1.2.840.113556.1.4.803:=2))" `
  26.         -SearchBase $RemoteOU -Properties company, department, displayName, givenName, mail, physicalDeliveryOfficeName, sn, telephoneNumber, title, pager, ipPhone, legacyExchangeDN
  27.  
  28.         $RGroups = Get-ADGroup -Server $RemoteDC -LDAPFilter "(mail=*)" -Properties displayname, mail, department, company, telephoneNumber, legacyExchangeDN
  29.     }
  30. } Catch {
  31.     Write-Output "Cant's connect to Remote DC!"
  32.     break
  33. }
  34.  
  35. #Generate list of remote users with local OU
  36. $RCheck = $RUsers | select @{e={"CN=$($_.Name),$LocalOU"};Label="DistinguishedName"}
  37. $RCheck += $RGroups | select @{e={"CN=$($_.Name),$LocalOU"};Label="DistinguishedName"}
  38.  
  39. #Get local contacts
  40. $LUsers = Get-ADObject -LDAPFilter "(mail=*)" -SearchBase $LocalOU -prop mail
  41.  
  42. #Remove local contacts that no longer exist in remote OU
  43. $Remove = $LUsers | ?{$RCheck.DistinguishedName -notcontains $_.DistinguishedName}
  44. If($Remove.Count -gt $MaxDeletions) {
  45.     Write-Output "Too many deletions $($Remove.Count), something may be wrong!"
  46.     break
  47. }
  48. $LUsers | ?{$RCheck.DistinguishedName -notcontains $_.DistinguishedName} | %{ Remove-MailContact $_.DistinguishedName -confirm:$false }
  49.  
  50. #Create/update local contacts with values from remote users
  51. ForEach ($RUser in $RUsers) {
  52.     If(!$ScheduledTask) { Write-Host $RUser.DistinguishedName }
  53.     $ContactDN = "CN={0},{1}" -f $RUser.Name, $LocalOU
  54.     $contact = Get-MailContact -Identity $ContactDN -ErrorAction SilentlyContinue
  55.     $recip = Get-Recipient $ruser.mail -ErrorAction SilentlyContinue
  56.     $targetAddress = $ruser.mail
  57.     if($TargetDomain -ne $null) {
  58.         $targetAddress = "$($ruser.samAccountName)@$TargetDomain"
  59.     }
  60.     If($null -eq $contact -and $null -eq $recip) {
  61.         Write-Host "Creating contact $($RUser.DistinguishedName)" -ForegroundColor Cyan
  62.         $contact = New-MailContact -OrganizationalUnit $LocalOU -FirstName $RUser.givenName -LastName $RUser.sn -Name $RUser.Name -DisplayName $RUser.Name -ExternalEmailAddress $targetAddress -PrimarySmtpAddress $RUser.mail
  63.         #Start-Sleep 10
  64.     }
  65.     If($null -ne $contact) {
  66.         $recip = Get-Recipient $ruser.mail -ErrorAction SilentlyContinue
  67.         If($null -eq $recip) {
  68.             Set-ADObject -identity $contact.DistinguishedName -add @{'proxyAddresses'="smtp:$($ruser.mail)"}
  69.             #Start-Sleep 10
  70.             $recip = Get-Recipient $ruser.mail -ErrorAction SilentlyContinue
  71.         }
  72.         if($null -ne $recip) {
  73.             if($recip.RecipientType -eq "MailContact") {
  74.                 $adobjfound = $false
  75.                 Try {
  76.                     $ado = Get-ADObject -Identity $contact.DistinguishedName -ErrorAction SilentlyContinue
  77.                     $adobjfound = $true
  78.                 }
  79.                 Catch {
  80.                     #Can't get AD Object yet, try again later.
  81.                 }
  82.                 if ($adobjfound) {
  83.                     $DisplayName = $RUser.displayName
  84.                     if($null -eq $DisplayName) { $DisplayName = $RUser.Name}
  85.                     $X500 = $RUser.legacyExchangeDN.Replace("'", "")
  86.                     Set-MailContact $contact.DistinguishedName -EmailAddressPolicyEnabled:$false -DisplayName $DisplayName -WarningAction SilentlyContinue
  87.                     Set-Contact $contact.DistinguishedName -Company $RUser.company -Department $RUser.department -FirstName $RUser.givenName -LastName $RUser.sn -Office $RUser.physicalDeliveryOfficeName -Phone $RUser.telephoneNumber -Title $RUser.title -Pager $RUser.pager -WarningAction SilentlyContinue
  88.                     if($null -ne $ado) {
  89.                         if($null -ne $RUser.legacyExchangeDN) { Set-ADObject $ado -add @{'proxyAddresses'="x500:$X500"} }
  90.                         if($null -ne $RUser.ipPhone -and $contact.ipPhone -ne $RUser.ipPhone) {
  91.                             Set-ADObject $ado -clear ipPhone -ErrorAction SilentlyContinue
  92.                             Set-ADObject $ado -add @{'ipPhone'=$RUser.ipPhone}
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }
  100.  
  101. ForEach($RGroup in $RGroups) {
  102.     If(!$ScheduledTask) { Write-Host $RGroup.DistinguishedName }
  103.     $ContactDN = "CN={0},{1}" -f $RGroup.Name, $LocalOU
  104.     $contact = Get-MailContact -Identity $ContactDN -ErrorAction SilentlyContinue
  105.     $recip = Get-Recipient $RGroup.mail -ErrorAction SilentlyContinue
  106.     $targetAddress = $RGroup.mail
  107.     $samAccountName = $RGroup.samAccountName.Replace(" ", "")
  108.     if($TargetDomain -ne $null) {
  109.         $targetAddress = "$samAccountName@$TargetDomain"
  110.     }
  111.     If($null -eq $contact -and $null -eq $recip) {
  112.         Write-Host "Creating contact $($RGroup.DistinguishedName)" -ForegroundColor Cyan
  113.         $contact = New-MailContact -OrganizationalUnit $LocalOU -FirstName $RGroup.Name -Name $RGroup.Name -DisplayName $RGroup.Name -ExternalEmailAddress $targetAddress -PrimarySmtpAddress $RGroup.mail
  114.         #Start-Sleep 10
  115.     }
  116.     If($null -ne $contact) {
  117.         $recip = Get-Recipient $RGroup.mail -ErrorAction SilentlyContinue
  118.         If($null -eq $recip) {
  119.             Set-ADObject -identity $contact.DistinguishedName -add @{'proxyAddresses'="smtp:$($RGroup.mail)"}
  120.             #Start-Sleep 10
  121.             $recip = Get-Recipient $RGroup.mail -ErrorAction SilentlyContinue
  122.         }
  123.         if($null -ne $recip) {
  124.             if($recip.RecipientType -eq "MailContact") {
  125.                 $adobjfound = $false
  126.                 Try {
  127.                     $ado = Get-ADObject -Identity $contact.DistinguishedName -ErrorAction SilentlyContinue
  128.                     $adobjfound = $true
  129.                 }
  130.                 Catch {
  131.                     #Can't get AD Object yet, try again later.
  132.                 }
  133.                 if ($adobjfound) {
  134.                     $DisplayName = $RGroup.displayName
  135.                     $X500 = $RGroup.legacyExchangeDN.Replace("'", "")
  136.                     if($null -eq $DisplayName) { $DisplayName = $RGroup.Name}
  137.                     Set-MailContact $contact.DistinguishedName -EmailAddressPolicyEnabled:$false -DisplayName $DisplayName -WarningAction SilentlyContinue
  138.                     Set-Contact $contact.DistinguishedName -Company $RGroup.company -Department $RGroup.department -FirstName $RGroup.DisplayName -Phone $RGroup.telephoneNumber -WarningAction SilentlyContinue
  139.                     if($null -ne $ado) {
  140.                         if($null -ne $RGroup.legacyExchangeDN) { Set-ADObject $ado -add @{'proxyAddresses'="x500:$X500"} }
  141.                     }
  142.                 }
  143.             }
  144.         }
  145.     }
  146. }
  147.  
  148. #Update address list (if supplied)
  149. if($AddressList -ne "") {
  150.     Get-AddressList -Identity $AddressList | Update-AddressList
  151. }
  152.  
  153. If($ScheduledTask) { Stop-Transcript }
  154.  
  155.  
Advertisement
Add Comment
Please, Sign In to add comment