Handle duplicate entries - ACTV status takes priority over INAC/LEFT

This commit is contained in:
2026-02-25 12:00:23 +11:00
parent 11e67bab4b
commit 0fdf3d0c01

View File

@@ -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()