From 38323e6be1a8ff0df8f7c54450ea2ad7d37fcef1 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Wed, 15 Apr 2026 14:39:14 -0700 Subject: [PATCH] Add support for SerialComputerNames CSV mapping Introduces a new `SerialComputerNames` device naming mode that allows automated device naming during deployment based on the BIOS serial number. The mapping is provided via a CSV file with `SerialNumber` and `ComputerName` columns. This feature requires `CopyUnattend` and writes a `SerialComputerNames.csv` file to the USB deployment media, replacing the need for manual prompts or prefix selection when device serial numbers are known in advance. The UI has been updated to support creating, loading, and saving the CSV mapping content. --- FFUDevelopment/BuildFFUVM.ps1 | 72 +++++++- FFUDevelopment/BuildFFUVM_UI.ps1 | 16 ++ FFUDevelopment/BuildFFUVM_UI.xaml | 18 ++ .../FFUUI.Core/FFUUI.Core.Config.psm1 | 13 +- .../FFUUI.Core/FFUUI.Core.Handlers.psm1 | 154 +++++++++++++++++- .../FFUUI.Core/FFUUI.Core.Initialize.psm1 | 11 +- FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 | 3 + FFUDevelopment/config/Sample_default.json | Bin 6760 -> 7042 bytes .../unattend/SampleSerialComputerNames.csv | 4 + docs/build.md | 41 ++++- docs/parameters_reference.md | 4 +- docs/quickstart.md | 14 ++ 12 files changed, 335 insertions(+), 15 deletions(-) create mode 100644 FFUDevelopment/unattend/SampleSerialComputerNames.csv diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index fe09363..d37ff13 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -73,7 +73,7 @@ When set to $true, will copy the provisioning package from the $FFUDevelopmentPa When set to $true, stages the selected architecture-specific unattend XML file as Unattend.xml on the deployment partition of the USB drive. Default is $false. .PARAMETER DeviceNamingMode -Controls how device naming is handled when unattend content is copied to USB media or injected into the FFU. Supported values are Legacy, None, Prompt, Template, and Prefixes. +Controls how device naming is handled when unattend content is copied to USB media or injected into the FFU. Supported values are Legacy, None, Prompt, Template, Prefixes, and SerialComputerNames. .PARAMETER DeviceNameTemplate Sets the device name used when DeviceNamingMode is Template. Supports a static name or the %serial% token when CopyUnattend is used. @@ -84,6 +84,12 @@ Sets the prefixes used when DeviceNamingMode is Prefixes. Each entry becomes a l .PARAMETER DeviceNamePrefixesPath Path to the source prefixes file used for legacy copy or when DeviceNamePrefixes is not supplied. Default is $FFUDevelopmentPath\Unattend\prefixes.txt. +.PARAMETER DeviceNameSerialComputerNames +Sets the CSV content used when DeviceNamingMode is SerialComputerNames. The CSV must include SerialNumber and ComputerName headers. + +.PARAMETER DeviceNameSerialComputerNamesPath +Path to the source CSV file used when DeviceNamingMode is SerialComputerNames and DeviceNameSerialComputerNames is not supplied. Default is $FFUDevelopmentPath\Unattend\SerialComputerNames.csv. + .PARAMETER UnattendX64FilePath Path to the x64 unattend XML source file. Default is $FFUDevelopmentPath\Unattend\unattend_x64.xml. @@ -425,11 +431,13 @@ param( [bool]$AllowVHDXCaching, [bool]$CopyPPKG, [bool]$CopyUnattend, - [ValidateSet('Legacy', 'None', 'Prompt', 'Template', 'Prefixes')] + [ValidateSet('Legacy', 'None', 'Prompt', 'Template', 'Prefixes', 'SerialComputerNames')] [string]$DeviceNamingMode = 'Legacy', [string]$DeviceNameTemplate, [string[]]$DeviceNamePrefixes, [string]$DeviceNamePrefixesPath, + [string[]]$DeviceNameSerialComputerNames, + [string]$DeviceNameSerialComputerNamesPath, [string]$UnattendX64FilePath, [string]$UnattendArm64FilePath, [bool]$CopyAutopilot, @@ -644,7 +652,7 @@ function Save-StagedUnattendFile { [Parameter(Mandatory = $true)] [string]$DestinationPath, [Parameter(Mandatory = $true)] - [ValidateSet('Legacy', 'None', 'Prompt', 'Template', 'Prefixes')] + [ValidateSet('Legacy', 'None', 'Prompt', 'Template', 'Prefixes', 'SerialComputerNames')] [string]$DeviceNamingMode, [string]$DeviceNameTemplate, [Parameter(Mandatory = $true)] @@ -685,6 +693,11 @@ function Save-StagedUnattendFile { $computerNamePath.ComputerNameElement.InnerText = '*' } } + elseif ($DeviceNamingMode -eq 'SerialComputerNames') { + if ($computerNamePath.CreatedComputerNameElement) { + $computerNamePath.ComputerNameElement.InnerText = '*' + } + } elseif (($DeviceNamingMode -eq 'Legacy') -and $computerNamePath.CreatedComputerNameElement) { $computerNamePath.ComputerNameElement.InnerText = if ($LegacyPrefixesWillBeStaged) { '*' } else { 'MyComputer' } } @@ -707,12 +720,24 @@ $resolvedDeviceNamePrefixesPath = if ([string]::IsNullOrWhiteSpace($DeviceNamePr else { $DeviceNamePrefixesPath } +$effectiveDeviceNameSerialComputerNames = @($DeviceNameSerialComputerNames | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Trim() }) +$resolvedDeviceNameSerialComputerNamesPath = if ([string]::IsNullOrWhiteSpace($DeviceNameSerialComputerNamesPath)) { + Join-Path (Join-Path $FFUDevelopmentPath 'Unattend') 'SerialComputerNames.csv' +} +else { + $DeviceNameSerialComputerNamesPath +} if (($DeviceNamingMode -eq 'Prefixes') -and ($effectiveDeviceNamePrefixes.Count -eq 0) -and (Test-Path -Path $resolvedDeviceNamePrefixesPath -PathType Leaf)) { $effectiveDeviceNamePrefixes = @(Get-Content -Path $resolvedDeviceNamePrefixesPath | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Trim() }) WriteLog "Loaded device name prefixes from $resolvedDeviceNamePrefixesPath" } +if (($DeviceNamingMode -eq 'SerialComputerNames') -and ($effectiveDeviceNameSerialComputerNames.Count -eq 0) -and (Test-Path -Path $resolvedDeviceNameSerialComputerNamesPath -PathType Leaf)) { + $effectiveDeviceNameSerialComputerNames = @(Get-Content -Path $resolvedDeviceNameSerialComputerNamesPath | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Trim() }) + WriteLog "Loaded serial computer-name mappings from $resolvedDeviceNameSerialComputerNamesPath" +} + if ($CopyUnattend -and $InjectUnattend) { throw 'CopyUnattend and InjectUnattend cannot both be set to `$true. Select only one unattend delivery method.' } @@ -749,6 +774,38 @@ elseif ($DeviceNamingMode -eq 'Prefixes') { throw 'DeviceNamingMode Prefixes requires at least one DeviceNamePrefixes entry or a valid DeviceNamePrefixesPath.' } } +elseif ($DeviceNamingMode -eq 'SerialComputerNames') { + if (-not $CopyUnattend) { + throw 'DeviceNamingMode SerialComputerNames requires CopyUnattend. Serial-to-computer-name mapping is not supported with InjectUnattend.' + } + + if ($effectiveDeviceNameSerialComputerNames.Count -eq 0) { + throw 'DeviceNamingMode SerialComputerNames requires DeviceNameSerialComputerNames content or a valid DeviceNameSerialComputerNamesPath.' + } + + try { + $serialComputerNameMappings = @($effectiveDeviceNameSerialComputerNames | ConvertFrom-Csv -ErrorAction Stop) + } + catch { + throw "DeviceNamingMode SerialComputerNames requires valid CSV content with SerialNumber and ComputerName headers. $($_.Exception.Message)" + } + + if ($serialComputerNameMappings.Count -eq 0) { + throw 'DeviceNamingMode SerialComputerNames requires at least one CSV data row.' + } + + $serialComputerNameHeaders = @($serialComputerNameMappings[0].PSObject.Properties.Name) + if ((-not ($serialComputerNameHeaders -contains 'SerialNumber')) -or (-not ($serialComputerNameHeaders -contains 'ComputerName'))) { + throw 'DeviceNamingMode SerialComputerNames requires SerialNumber and ComputerName headers.' + } + + $validSerialComputerNameMappings = @($serialComputerNameMappings | Where-Object { + -not [string]::IsNullOrWhiteSpace([string]$_.SerialNumber) -and -not [string]::IsNullOrWhiteSpace([string]$_.ComputerName) + }) + if ($validSerialComputerNameMappings.Count -eq 0) { + throw 'DeviceNamingMode SerialComputerNames requires at least one row with both SerialNumber and ComputerName values.' + } +} # Validate that the selected Windows SKU is compatible with the chosen Windows release and ensure an ISO is provided for unsupported releases $clientSKUs = @( @@ -4570,6 +4627,11 @@ Function New-DeploymentUSB { $computerNamePath.ComputerNameElement.InnerText = '*' } } + elseif ($DeviceNamingMode -eq 'SerialComputerNames') { + if ($computerNamePath.CreatedComputerNameElement) { + $computerNamePath.ComputerNameElement.InnerText = '*' + } + } elseif (($DeviceNamingMode -eq 'Legacy') -and $computerNamePath.CreatedComputerNameElement) { $computerNamePath.ComputerNameElement.InnerText = if ($LegacyPrefixesWillBeStaged) { '*' } else { 'MyComputer' } } @@ -4644,6 +4706,10 @@ Function New-DeploymentUSB { WriteLog "Writing prefixes.txt file to $UnattendPathOnUSB" $using:effectiveDeviceNamePrefixes | Set-Content -Path (Join-Path $UnattendPathOnUSB 'prefixes.txt') -Encoding UTF8 } + elseif ($using:DeviceNamingMode -eq 'SerialComputerNames') { + WriteLog "Writing SerialComputerNames.csv file to $UnattendPathOnUSB" + $using:effectiveDeviceNameSerialComputerNames | Set-Content -Path (Join-Path $UnattendPathOnUSB 'SerialComputerNames.csv') -Encoding UTF8 + } elseif ($legacyPrefixesWillBeStaged) { WriteLog "Copying prefixes.txt file to $UnattendPathOnUSB" Copy-Item -Path $using:resolvedDeviceNamePrefixesPath -Destination (Join-Path $UnattendPathOnUSB 'prefixes.txt') -Force | Out-Null diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index f2503d6..2dd3c1e 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -528,6 +528,22 @@ $script:uiState.Controls.btnRun.Add_Click({ return } } + elseif ($config.DeviceNamingMode -eq 'SerialComputerNames') { + if (-not $config.CopyUnattend) { + [System.Windows.MessageBox]::Show("Select Copy Unattend.xml before using 'Specify Serial to Device Name Mapping'.", "Copy Unattend Required", "OK", "Warning") | Out-Null + $btnRun.IsEnabled = $true + $script:uiState.Controls.txtStatus.Text = "Build canceled: serial computer-name mapping requires Copy Unattend.xml." + return + } + + $hasSavedSerialComputerNamesPath = -not [string]::IsNullOrWhiteSpace([string]$config.DeviceNameSerialComputerNamesPath) -and (Test-Path -Path $config.DeviceNameSerialComputerNamesPath -PathType Leaf) + if ((($null -eq $config.DeviceNameSerialComputerNames) -or ($config.DeviceNameSerialComputerNames.Count -eq 0)) -and -not $hasSavedSerialComputerNamesPath) { + [System.Windows.MessageBox]::Show("Enter CSV content or choose a valid SerialComputerNames.csv file before using 'Specify Serial to Device Name Mapping'.", "Serial Mapping Required", "OK", "Warning") | Out-Null + $btnRun.IsEnabled = $true + $script:uiState.Controls.txtStatus.Text = "Build canceled: serial computer-name mapping required." + return + } + } $configFilePath = Join-Path $config.FFUDevelopmentPath "\config\FFUConfig.json" # Sort top-level keys alphabetically for consistent output diff --git a/FFUDevelopment/BuildFFUVM_UI.xaml b/FFUDevelopment/BuildFFUVM_UI.xaml index e1977cb..7de9f6d 100644 --- a/FFUDevelopment/BuildFFUVM_UI.xaml +++ b/FFUDevelopment/BuildFFUVM_UI.xaml @@ -963,6 +963,24 @@