Set-MachineNumber: handle duplicate-PC reassignment (real -> real)

Tech catches a PC imaged with a wrong machine number. Previously the
share restore (NTLARS .reg + UDC settings + UDC live data) only fired
on the placeholder->real transition, so a real->real change rewrote
only UDC JSON, eDNC reg, and MTConnect Devices.xml - leaving the wrong
NTLARS config in place.

Update-MachineNumber.ps1: replace the placeholder-only guard with an
any-change guard so the share restore block fires on reassign too.
The existing one-shot migrated/ consumption keeps live-data restore
idempotent. Also writes C:\Enrollment\machine-number.txt to keep
imaging-time scripts in sync.

Set-MachineNumber.ps1 (both collections + nocollections): show a
confirmation dialog when reassigning between two real numbers, naming
old/new and listing what gets pulled. Audit each call to
C:\Logs\Shopfloor\reassign.log.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
cproudlock
2026-05-12 15:13:30 -04:00
parent c8a0f98be1
commit 3896667c90
3 changed files with 112 additions and 7 deletions

View File

@@ -60,16 +60,19 @@ function Update-MachineNumber {
[string]$Site = 'West Jefferson' [string]$Site = 'West Jefferson'
) )
$out = @{ UdcUpdated = $false; EdncUpdated = $false; Errors = @(); RegImported = $null } $out = @{ UdcUpdated = $false; EdncUpdated = $false; Errors = @(); RegImported = $null; OldUdc = $null; OldEdnc = $null }
# --- If UDC or eDNC is still at placeholder 9999, try to pull the # If the machine number is changing (placeholder->real OR real->real
# per-machine .reg backup from the SFLD share and restore all # reassignment after a duplicate-image), pull per-machine state for the
# the eFocas/PPDCS/Hssb config. The tech-typed $NewNumber is still # new number from the SFLD share: NTLARS .reg, UDC settings, UDC live
# written last (below), so the restore never clobbers it. --- # data. The live-data restore is idempotent via one-shot migrated/
# consumption, so it stays safe on reassign too.
$current = Get-CurrentMachineNumber $current = Get-CurrentMachineNumber
$isPlaceholder = (($current.Udc -in @('9999', $null, '')) -or ($current.Ednc -in @('9999', $null, ''))) $out.OldUdc = $current.Udc
$out.OldEdnc = $current.Ednc
$isChanging = ($current.Udc -ne $NewNumber) -or ($current.Ednc -ne $NewNumber)
if ($isPlaceholder -and $NewNumber -ne '9999') { if ($isChanging -and $NewNumber -ne '9999') {
$sharePath = $null $sharePath = $null
$siteCfgPath = 'C:\Enrollment\site-config.json' $siteCfgPath = 'C:\Enrollment\site-config.json'
if (Test-Path $siteCfgPath) { if (Test-Path $siteCfgPath) {
@@ -329,5 +332,19 @@ function Update-MachineNumber {
} }
} }
# Keep C:\Enrollment\machine-number.txt in sync. Post-imaging GE-Enforce
# prefers eDNC reg, but imaging-time scripts (Install-FromManifest
# TargetMachineNumbers filter, 01-eDNC.ps1, 03-RestoreEDncConfig.ps1)
# still read this file. Avoid drift on reassign.
try {
$mnFile = 'C:\Enrollment\machine-number.txt'
$mnDir = Split-Path -Parent $mnFile
if (-not (Test-Path $mnDir)) { New-Item -ItemType Directory -Path $mnDir -Force | Out-Null }
Set-Content -Path $mnFile -Value $NewNumber -Encoding UTF8 -NoNewline
$out.MachineNumberTxtUpdated = $true
} catch {
$out.Errors += "machine-number.txt update failed: $_"
}
return $out return $out
} }

View File

@@ -56,8 +56,52 @@ if ($new -notmatch '^\d+$') {
exit 1 exit 1
} }
# --- Reassign confirmation (real -> real). Placeholder (9999/null) skips. ---
$currentIsReal = (($currentUdc -and $currentUdc -ne '9999') -or
($currentEdnc -and $currentEdnc -ne '9999'))
$differsFromNew = ($currentUdc -ne $new) -or ($currentEdnc -ne $new)
if ($currentIsReal -and $differsFromNew) {
$oldDisplay = if ($currentUdc) { $currentUdc } else { $currentEdnc }
$confirmMsg = @(
"Reassign machine number from $oldDisplay to $new ?"
""
"This will pull from the SFLD share for $new :"
" - NTLARS .reg backup (eFocas / PPDCS / Hssb)"
" - UDC settings (udc_settings_$new.json)"
" - UDC live data backup (if present)"
""
"Use this only when the PC was imaged with the wrong number."
) -join "`n"
$confirm = [System.Windows.Forms.MessageBox]::Show(
$confirmMsg,
"Confirm Reassignment",
[System.Windows.Forms.MessageBoxButtons]::OKCancel,
[System.Windows.Forms.MessageBoxIcon]::Warning
)
if ($confirm -ne [System.Windows.Forms.DialogResult]::OK) {
Write-Host "Reassignment cancelled."
exit 0
}
}
$mnResult = Update-MachineNumber -NewNumber $new -Site $site $mnResult = Update-MachineNumber -NewNumber $new -Site $site
# --- Audit log: one line per machine-number change. Best-effort. ---
try {
$logDir = 'C:\Logs\Shopfloor'
if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }
$logFile = Join-Path $logDir 'reassign.log'
$now = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
$whoami = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$oldDisp = if ($mnResult.OldUdc) { $mnResult.OldUdc } elseif ($mnResult.OldEdnc) { $mnResult.OldEdnc } else { '(none)' }
$regFlag = if ($mnResult.RegImported) { 'OK' } else { 'skip' }
$udcFlag = if ($mnResult.UdcSettingsRestored) { 'OK' } else { 'skip' }
$dataFlag = if ($mnResult.UdcRestored) { 'OK' } else { 'skip' }
$mtcCnt = @($mnResult.MTConnectUpdated).Count
$line = "$now $env:COMPUTERNAME user=$whoami $oldDisp -> $new udc=$($mnResult.UdcUpdated) ednc=$($mnResult.EdncUpdated) reg=$regFlag settings=$udcFlag data=$dataFlag mtc=$mtcCnt"
Add-Content -Path $logFile -Value $line -Encoding UTF8
} catch {}
$results = @() $results = @()
if ($mnResult.UdcUpdated) { if ($mnResult.UdcUpdated) {
Write-Host "UDC: $currentUdc -> $new" Write-Host "UDC: $currentUdc -> $new"

View File

@@ -56,8 +56,52 @@ if ($new -notmatch '^\d+$') {
exit 1 exit 1
} }
# --- Reassign confirmation (real -> real). Placeholder (9999/null) skips. ---
$currentIsReal = (($currentUdc -and $currentUdc -ne '9999') -or
($currentEdnc -and $currentEdnc -ne '9999'))
$differsFromNew = ($currentUdc -ne $new) -or ($currentEdnc -ne $new)
if ($currentIsReal -and $differsFromNew) {
$oldDisplay = if ($currentUdc) { $currentUdc } else { $currentEdnc }
$confirmMsg = @(
"Reassign machine number from $oldDisplay to $new ?"
""
"This will pull from the SFLD share for $new :"
" - NTLARS .reg backup (eFocas / PPDCS / Hssb)"
" - UDC settings (udc_settings_$new.json)"
" - UDC live data backup (if present)"
""
"Use this only when the PC was imaged with the wrong number."
) -join "`n"
$confirm = [System.Windows.Forms.MessageBox]::Show(
$confirmMsg,
"Confirm Reassignment",
[System.Windows.Forms.MessageBoxButtons]::OKCancel,
[System.Windows.Forms.MessageBoxIcon]::Warning
)
if ($confirm -ne [System.Windows.Forms.DialogResult]::OK) {
Write-Host "Reassignment cancelled."
exit 0
}
}
$mnResult = Update-MachineNumber -NewNumber $new -Site $site $mnResult = Update-MachineNumber -NewNumber $new -Site $site
# --- Audit log: one line per machine-number change. Best-effort. ---
try {
$logDir = 'C:\Logs\Shopfloor'
if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }
$logFile = Join-Path $logDir 'reassign.log'
$now = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
$whoami = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$oldDisp = if ($mnResult.OldUdc) { $mnResult.OldUdc } elseif ($mnResult.OldEdnc) { $mnResult.OldEdnc } else { '(none)' }
$regFlag = if ($mnResult.RegImported) { 'OK' } else { 'skip' }
$udcFlag = if ($mnResult.UdcSettingsRestored) { 'OK' } else { 'skip' }
$dataFlag = if ($mnResult.UdcRestored) { 'OK' } else { 'skip' }
$mtcCnt = @($mnResult.MTConnectUpdated).Count
$line = "$now $env:COMPUTERNAME user=$whoami $oldDisp -> $new udc=$($mnResult.UdcUpdated) ednc=$($mnResult.EdncUpdated) reg=$regFlag settings=$udcFlag data=$dataFlag mtc=$mtcCnt"
Add-Content -Path $logFile -Value $line -Encoding UTF8
} catch {}
$results = @() $results = @()
if ($mnResult.UdcUpdated) { if ($mnResult.UdcUpdated) {
Write-Host "UDC: $currentUdc -> $new" Write-Host "UDC: $currentUdc -> $new"