Skip to content

Commit

Permalink
feat: Add synchronization of O365 contacts
Browse files Browse the repository at this point in the history
This commit adds the functionality to synchronize O365 contacts with a specified list of source objects. The code now includes the conversion of source objects to contact objects and checks if the contact already exists in O365. If the contact does not exist, a new contact is created. If the contact already exists, the code checks if the contact data is the same and updates it if necessary.
  • Loading branch information
PrzemyslawKlys committed Sep 24, 2023
1 parent df80022 commit edf0d1c
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions Public/Sync-O365Contact.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
param(
[Parameter(Mandatory)][Array] $SourceObjects,
[Parameter()][Array] $Domains
)

)
Write-Color -Text "[i] ", "Starting synchronization of ", $SourceObjects.Count, " objects" -Color Yellow, White, Cyan, White, Cyan

$CurrentContactsCache = [ordered]@{}
$SourceObjectsCache = [ordered]@{}

if (-not $Domains) {
Expand All @@ -25,59 +24,48 @@
}
}

try {
$CurrentContacts = Get-MailContact -ResultSize Unlimited -ErrorAction Stop
} catch {
Write-Color -Text "[e] ", "Failed to get current contacts. Error: ", ($_.Exception.Message -replace ([Environment]::NewLine), " " )-Color Yellow, White, Red
return
[Array] $ConvertedObjects = foreach ($Source in $SourceObjects) {
Convert-GraphObjectToContact -SourceObject $Source
}
foreach ($Contact in $CurrentContacts) {
$Found = $false
foreach ($Domain in $Domains) {
if ($Contact.PrimarySmtpAddress -notlike "*@$Domain") {
continue
} else {
$Found = $true
}
}
if ($Found) {
$CurrentContactsCache[$Contact.PrimarySmtpAddress] = $Contact
}

$CurrentContactsCache = Get-O365ContactsFromTenant -Domains $Domains
if ($null -eq $CurrentContactsCache) {
return
}

foreach ($Source in $SourceObjects) {
if ($Source.Mail) {
foreach ($Object in $ConvertedObjects) {
$Source = $Object.MailContact
$SourceContact = $Object.Contact
if ($Source.PrimarySmtpAddress) {
# we only process contacts if it has mail
$Skip = $true
foreach ($Domain in $Domains) {
if ($Source.Mail -like "*@$Domain") {
if ($Source.PrimarySmtpAddress -like "*@$Domain") {
$Skip = $false
break
}
}
if ($Skip) {
Write-Color -Text "[s] ", "Skipping ", $Source.DisplayName, " / ", $Source.Mail, " as it's not in domains to synchronize ", $($Domains -join ', ') -Color Yellow, White, Red, White, Red
Write-Color -Text "[s] ", "Skipping ", $Source.DisplayName, " / ", $Source.PrimarySmtpAddress, " as it's not in domains to synchronize ", $($Domains -join ', ') -Color Yellow, White, Red, White, Red
continue
}
# We cache all sources to make sure we can remove users later on
$SourceObjectsCache[$Source.PrimarySmtpAddress] = $Source

$SourceObjectsCache[$Source.Mail] = $Source

if ($CurrentContactsCache[$Source.Mail]) {
Write-Color -Text "[i] ", "Skipping ", $Source.DisplayName, " / ", $Source.Mail, " as it already exists" -Color Yellow, White, DarkCyan, White, Cyan
continue
}
Write-Color -Text "[i] ", "Processing ", $Source.DisplayName, " / ", $Source.Mail -Color Yellow, White, Cyan, White, Cyan
try {
New-MailContact -DisplayName $Source.DisplayName -ExternalEmailAddress $Source.Mail -Name $Source.DisplayName -WhatIf:$WhatIfPreference -ErrorAction Stop
} catch {
Write-Color -Text "[e] ", "Failed to create contact. Error: ", ($_.Exception.Message -replace ([Environment]::NewLine), " " )-Color Yellow, White, Red
if ($CurrentContactsCache[$Source.PrimarySmtpAddress]) {
# Contact already exists, but lets check if the data is the same
Set-O365OrgContact -CurrentContactsCache $CurrentContactsCache -Source $Source -SourceContact $SourceContact
} else {
# Contact is new
New-O365OrgContact -Source $Source
}
} else {
#Write-Color -Text "[i] ", "Processing stopped, as no email ", $Source.DisplayName -Color Yellow, White, Red
#New-Contact -DisplayName $Source.DisplayName -Name $Source.DisplayName -WhatIf:$WhatIfPreference
}
}
foreach ($C in $CurrentContactsCache.Keys) {
$Contact = $CurrentContactsCache[$C]
$Contact = $CurrentContactsCache[$C].MailContact
if ($SourceObjectsCache[$Contact.PrimarySmtpAddress]) {
continue
} else {
Expand Down

0 comments on commit edf0d1c

Please sign in to comment.