Handle duplicate entries - ACTV status takes priority over INAC/LEFT
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
# Update-StaffAD.ps1
|
# Update-StaffAD.ps1
|
||||||
# Reads Staff_Extract.csv and updates AD email addresses and account status.
|
# Reads Staff_Extract.csv and updates AD email addresses and account status.
|
||||||
# Matches users by sAMAccountName (PAYROLL_REC_NO column).
|
# 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.
|
# Applies changes automatically and appends results to a log file.
|
||||||
|
|
||||||
# --- Configuration ---
|
# --- Configuration ---
|
||||||
@@ -29,6 +30,18 @@ function Write-Log {
|
|||||||
Add-Content -Path $LogFile -Value $Message
|
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 ---
|
# --- Run Header ---
|
||||||
Write-Log ""
|
Write-Log ""
|
||||||
Write-Log $RunHeader
|
Write-Log $RunHeader
|
||||||
@@ -40,7 +53,7 @@ $disables = 0
|
|||||||
$noChanges = 0
|
$noChanges = 0
|
||||||
$notFound = 0
|
$notFound = 0
|
||||||
|
|
||||||
foreach ($entry in $Staff) {
|
foreach ($entry in $Deduped.Values) {
|
||||||
$samAccount = $entry.PAYROLL_REC_NO.Trim()
|
$samAccount = $entry.PAYROLL_REC_NO.Trim()
|
||||||
$status = $entry.STAFF_STATUS.Trim()
|
$status = $entry.STAFF_STATUS.Trim()
|
||||||
$email = $entry.E_MAIL.Trim()
|
$email = $entry.E_MAIL.Trim()
|
||||||
|
|||||||
Reference in New Issue
Block a user