Skip to content

Commit 7b16a98

Browse files
author
Matt
committed
Done. The new flow:
Process all contacts - create new ones immediately (no waiting per contact) Collect new contacts in $newlyCreatedContacts array After loop ends - wait 3 seconds once for Exchange replication Batch set CustomAttribute1 on all new contacts This should be significantly faster. Redeploy and test.
1 parent 8f0853d commit 7b16a98

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

SynapseContactSync/run.ps1

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,11 @@ function New-MailContactWithRetry {
7777
-ErrorAction Stop | Out-Null
7878

7979
Write-Host " Created contact with alias: $finalAlias"
80-
81-
# Brief pause to allow Exchange to propagate (reduced from 2s)
82-
Start-Sleep -Milliseconds 500
83-
84-
Set-MailContact -Identity $Email `
85-
-CustomAttribute1 $CustomAttributeValue `
86-
-ErrorAction Stop
87-
8880
$created = $true
8981

82+
# Return email for batch CustomAttribute1 update later
83+
return @{ Email = $Email; CustomAttributeValue = $CustomAttributeValue }
84+
9085
}
9186
catch {
9287
if ($_.Exception.Message -like "*proxy address*is already being used*" -or
@@ -164,6 +159,9 @@ function Sync-ContactsFromExcel {
164159
# Track emails in Excel for removal check later
165160
$excelEmails = @{}
166161

162+
# Collect newly created contacts for batch CustomAttribute1 update
163+
$newlyCreatedContacts = @()
164+
167165
foreach ($contact in $contacts) {
168166
try {
169167
# Handle both column name formats
@@ -220,15 +218,18 @@ function Sync-ContactsFromExcel {
220218
# Generate base alias from email prefix
221219
$baseAlias = $email.Split('@')[0] -replace '[^a-zA-Z0-9]', ''
222220

223-
# Create with retry logic for alias conflicts
224-
$finalAlias = New-MailContactWithRetry `
221+
# Create contact (CustomAttribute1 will be set in batch later)
222+
$newContact = New-MailContactWithRetry `
225223
-Email $email `
226224
-DisplayName $displayName `
227225
-BaseAlias $baseAlias `
228226
-FirstName $firstName `
229227
-LastName $lastName `
230228
-CustomAttributeValue $CustomAttributeValue
231229

230+
if ($newContact) {
231+
$newlyCreatedContacts += $newContact
232+
}
232233
$created++
233234
}
234235

@@ -262,6 +263,25 @@ function Sync-ContactsFromExcel {
262263
}
263264
}
264265

266+
# Batch update CustomAttribute1 for newly created contacts
267+
if ($newlyCreatedContacts.Count -gt 0) {
268+
Write-Host "`nSetting CustomAttribute1 on $($newlyCreatedContacts.Count) newly created contacts..."
269+
Start-Sleep -Seconds 3 # Give Exchange time to replicate
270+
271+
foreach ($newContact in $newlyCreatedContacts) {
272+
try {
273+
Set-MailContact -Identity $newContact.Email `
274+
-CustomAttribute1 $newContact.CustomAttributeValue `
275+
-ErrorAction Stop
276+
Write-Host " Set attribute on: $($newContact.Email)"
277+
}
278+
catch {
279+
Write-Warning " Failed to set attribute on $($newContact.Email): $_"
280+
$errors += "Failed to set CustomAttribute1 on $($newContact.Email): $_"
281+
}
282+
}
283+
}
284+
265285
# Remove contacts no longer in Excel (bidirectional sync)
266286
# Using $existingMailContacts already fetched at the start
267287
Write-Host "`nChecking for contacts to remove..."

0 commit comments

Comments
 (0)