From 0fdf3d0c01c36da1ecab1dc20d3fe324e090d971 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Wed, 25 Feb 2026 12:00:23 +1100 Subject: [PATCH] Handle duplicate entries - ACTV status takes priority over INAC/LEFT --- Update-StaffAD.ps1 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Update-StaffAD.ps1 b/Update-StaffAD.ps1 index d1985f3..cf92c3a 100644 --- a/Update-StaffAD.ps1 +++ b/Update-StaffAD.ps1 @@ -1,6 +1,7 @@ # Update-StaffAD.ps1 # Reads Staff_Extract.csv and updates AD email addresses and account status. # Matches users by sAMAccountName (PAYROLL_REC_NO column). +# If duplicate entries exist, ACTV takes priority over INAC/LEFT. # Applies changes automatically and appends results to a log file. # --- Configuration --- @@ -29,6 +30,18 @@ function Write-Log { Add-Content -Path $LogFile -Value $Message } +# --- Deduplicate: ACTV wins over INAC/LEFT --- +$Deduped = @{} +foreach ($entry in $Staff) { + $key = $entry.PAYROLL_REC_NO.Trim() + if (-not $Deduped.ContainsKey($key)) { + $Deduped[$key] = $entry + } elseif ($entry.STAFF_STATUS.Trim() -eq "ACTV") { + # ACTV always overrides whatever was stored + $Deduped[$key] = $entry + } +} + # --- Run Header --- Write-Log "" Write-Log $RunHeader @@ -40,7 +53,7 @@ $disables = 0 $noChanges = 0 $notFound = 0 -foreach ($entry in $Staff) { +foreach ($entry in $Deduped.Values) { $samAccount = $entry.PAYROLL_REC_NO.Trim() $status = $entry.STAFF_STATUS.Trim() $email = $entry.E_MAIL.Trim()