Files
cproudlock 3896667c90 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>
2026-05-12 15:13:30 -04:00

132 lines
5.4 KiB
PowerShell

# Set-MachineNumber.ps1 - Update UDC + eDNC machine number on a Standard shopfloor PC
#
# Purpose:
# Both UDC and eDNC use the same per-machine identifier ("Workstation Number" /
# "Machine Number"). On Standard PCs imaged via PXE preinstall, both are installed
# with a placeholder. When the PC is brought to its physical machine and assigned
# a real number, this helper updates both apps in one step.
#
# Persistence locations updated:
# 1. UDC: C:\ProgramData\UDC\udc_settings.json (GeneralSettings.MachineNumber)
# 2. eDNC: HKLM:\SOFTWARE\WOW6432Node\GE Aircraft Engines\DNC\General\MachineNo
#
# After updating, kills any running UDC.exe and relaunches it with the new args
# so the in-memory state matches the persisted value.
#
# Run as SupportUser (admin). Requires write access to ProgramData and HKLM.
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
. "$PSScriptRoot\..\Shopfloor\lib\Get-PCProfile.ps1"
. "$PSScriptRoot\..\Shopfloor\lib\Update-MachineNumber.ps1"
$site = if ($siteConfig) { $siteConfig.siteName } else { 'West Jefferson' }
# --- Read current values for display ---
$currentMN = Get-CurrentMachineNumber
$currentUdc = $currentMN.Udc
$currentEdnc = $currentMN.Ednc
# --- Show prompt with current state ---
$promptLines = @()
$promptLines += "Current UDC machine number: $(if ($currentUdc) { $currentUdc } else { '(not set)' })"
$promptLines += "Current eDNC machine number: $(if ($currentEdnc) { $currentEdnc } else { '(not set)' })"
$promptLines += ""
$promptLines += "Enter the new Machine Number for this PC:"
$prompt = $promptLines -join "`n"
$new = [Microsoft.VisualBasic.Interaction]::InputBox($prompt, "Set Machine Number", "")
if ([string]::IsNullOrWhiteSpace($new)) {
Write-Host "Cancelled."
exit 0
}
$new = $new.Trim()
# --- Validate: digits only (loosen if you need alphanumerics) ---
if ($new -notmatch '^\d+$') {
[System.Windows.Forms.MessageBox]::Show(
"Machine number must be digits only.`n`nYou entered: '$new'",
"Invalid Machine Number",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
) | Out-Null
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
# --- 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 = @()
if ($mnResult.UdcUpdated) {
Write-Host "UDC: $currentUdc -> $new"
$results += "UDC updated to $new"
} elseif (-not (Test-Path 'C:\ProgramData\UDC\udc_settings.json')) {
$results += "UDC: settings file missing (run UDC.exe once first)"
}
if ($mnResult.EdncUpdated) {
Write-Host "eDNC: $currentEdnc -> $new"
$results += "eDNC updated to $new"
} elseif (-not (Test-Path 'HKLM:\SOFTWARE\WOW6432Node\GE Aircraft Engines\DNC\General')) {
$results += "eDNC: registry key missing (eDNC not installed?)"
}
foreach ($err in $mnResult.Errors) {
Write-Warning $err
$results += $err
}
if ($mnResult.UdcUpdated) { Write-Host "UDC.exe relaunched." }
# --- Show summary ---
$summary = ($results -join "`n") + "`n`nTo apply eDNC changes, restart any running DncMain.exe."
[System.Windows.Forms.MessageBox]::Show(
$summary,
"Set Machine Number - Done",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Information
) | Out-Null