# select-cmm-bay.ps1 - Arrow-key CMM bay picker for imaging. # # Reads cmm-bay-config.csv from the PXE enrollment share and presents # a menu of CMM1-CMM12 with their PC-DMIS version and DODA flag. # Writes the selected CMM ID to $OutFile for startnet.cmd to read back. # # Also writes resolved config files so 09-Setup-CMM.ps1 can gate installs: # C:\Enrollment\cmm\version.txt (e.g. "2019") # C:\Enrollment\cmm\doda.txt (e.g. "yes" or "no") # # Exit codes: # 0 = CMM ID written to $OutFile # 1 = user cancelled (Esc) # 2 = CSV unreadable AND no fallback entered param( [string]$ConfigPath = 'Y:\installers-post\cmm\cmm-bay-config.csv', [Parameter(Mandatory=$true)][string]$OutFile ) $ErrorActionPreference = 'Continue' function Read-BayList { param([string]$Path) if (-not (Test-Path -LiteralPath $Path)) { return @() } try { return Import-Csv -LiteralPath $Path | Sort-Object { if ($_.cmm_id -match '(\d+)$') { [int]$Matches[1] } else { 999 } } } catch { return @() } } function Show-Menu { param( [object[]]$Items, [int]$Selected, [string]$Title ) Clear-Host Write-Host "" Write-Host " ========================================" Write-Host " $Title" Write-Host " ========================================" Write-Host "" Write-Host " Up / Down arrows = navigate, Enter = select, Esc = cancel" Write-Host "" for ($i = 0; $i -lt $Items.Count; $i++) { $item = $Items[$i] if ($item -is [string]) { $line = $item } else { $dodaLabel = if ($item.doda -ieq 'yes') { '+ DODA' } else { '' } $line = "{0,-8} PC-DMIS {1,-6} {2}" -f $item.cmm_id, $item.pcdmis_version, $dodaLabel } if ($i -eq $Selected) { Write-Host (" > " + $line) -ForegroundColor Black -BackgroundColor White } else { Write-Host (" " + $line) } } } $bays = @(Read-BayList -Path $ConfigPath) if ($bays.Count -eq 0) { Write-Host "WARNING: Could not read $ConfigPath" } $menuItems = @($bays) + @("Other (manual entry)") $selected = 0 while ($true) { Show-Menu -Items $menuItems -Selected $selected -Title "Select CMM Bay" $key = [System.Console]::ReadKey($true) switch ($key.Key) { 'UpArrow' { if ($selected -gt 0) { $selected-- } } 'DownArrow' { if ($selected -lt ($menuItems.Count - 1)) { $selected++ } } 'Enter' { break } 'Escape' { Write-Host "`n Cancelled."; exit 1 } } if ($key.Key -eq 'Enter') { break } } $chosen = $menuItems[$selected] if ($chosen -is [string]) { Write-Host "" $manual = Read-Host " Enter CMM ID (e.g. CMM1)" if (-not $manual) { Write-Host " No CMM ID entered." exit 2 } $cmmId = $manual.Trim().ToUpper() $match = $bays | Where-Object { $_.cmm_id -ieq $cmmId } if ($match) { $chosen = $match } else { Write-Host " $cmmId not in bay-config. Writing ID only (no version/doda resolution)." [System.IO.File]::WriteAllText($OutFile, $cmmId) exit 0 } } $cmmId = $chosen.cmm_id.Trim().ToUpper() [System.IO.File]::WriteAllText($OutFile, $cmmId) Write-Host "" Write-Host " Selected: $cmmId (PC-DMIS $($chosen.pcdmis_version), DODA=$($chosen.doda))" exit 0