From 417be73b23d9021a83a70309e51ae972800ad2a9 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Mon, 24 Nov 2025 16:46:57 -0800 Subject: [PATCH] Replace SerialNumber with UniqueId for USB drive identification Switches USB drive matching logic from relying on SerialNumber to using UniqueId, which provides more reliable and consistent device identification across different systems. Updates the Get-USBDrive function to retrieve UniqueId via Get-Disk and trims the machine name suffix (characters after colon) for consistent matching. The new approach first filters candidates by model and media type, then validates each candidate against the configured UniqueId. Reflects this change across the UI layer by updating column headers, configuration handling, and drive enumeration functions to use UniqueId instead of SerialNumber for saving and loading USB drive selections. --- FFUDevelopment/BuildFFUVM.ps1 | 36 ++++++++++++++----- FFUDevelopment/BuildFFUVM_UI.xaml | 2 +- .../FFUUI.Core/FFUUI.Core.Config.psm1 | 8 +++-- .../FFUUI.Core/FFUUI.Core.Initialize.psm1 | 14 ++++---- FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 | 28 +++++++++++---- 5 files changed, 63 insertions(+), 25 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM.ps1 b/FFUDevelopment/BuildFFUVM.ps1 index bfeea1b..8364070 100644 --- a/FFUDevelopment/BuildFFUVM.ps1 +++ b/FFUDevelopment/BuildFFUVM.ps1 @@ -3449,18 +3449,38 @@ Function Get-USBDrive { # Log the count of specified USB drives $USBDriveListCount = $USBDriveList.Count WriteLog "Looking for $USBDriveListCount USB drives from USB Drive List" - # Get only the specified USB drives based on both model and serial number + # Get only the specified USB drives based on model and UniqueId $USBDrives = @() foreach ($model in $USBDriveList.Keys) { - $serialNumber = $USBDriveList[$model] - Writelog "Looking for USB drive model $model with serial number $serialNumber" - $USBDrive = Get-CimInstance -ClassName Win32_DiskDrive -Filter "Model LIKE '%$model%' AND SerialNumber LIKE '$serialNumber%' AND (MediaType='Removable Media' OR MediaType='External hard disk media')" - if ($USBDrive) { - WriteLog "Found USB drive model $($USBDrive.model) with serial number $($USBDrive.serialNumber)" - $USBDrives += $USBDrive + $configUniqueId = $USBDriveList[$model] + WriteLog "Looking for USB drive model $model with UniqueId $configUniqueId" + # First get candidate drives by model and media type + $candidateDrives = Get-CimInstance -ClassName Win32_DiskDrive -Filter "Model LIKE '%$model%' AND (MediaType='Removable Media' OR MediaType='External hard disk media')" + $foundDrive = $null + foreach ($candidate in $candidateDrives) { + # Get the disk to retrieve UniqueId + $disk = Get-Disk -Number $candidate.Index -ErrorAction SilentlyContinue + if ($disk -and $disk.UniqueId) { + # Trim the machine name suffix (everything after the colon) from UniqueId + $diskUniqueId = if ($disk.UniqueId -match ':') { + $disk.UniqueId.Split(':')[0] + } + else { + $disk.UniqueId + } + # Match on the trimmed UniqueId + if ($diskUniqueId -eq $configUniqueId) { + $foundDrive = $candidate + break + } + } + } + if ($foundDrive) { + WriteLog "Found USB drive model $($foundDrive.Model) with UniqueId $configUniqueId" + $USBDrives += $foundDrive } else { - WriteLog "USB drive model $model with serial number $serialNumber not found" + WriteLog "USB drive model $model with UniqueId $configUniqueId not found" } } $USBDrivesCount = $USBDrives.Count diff --git a/FFUDevelopment/BuildFFUVM_UI.xaml b/FFUDevelopment/BuildFFUVM_UI.xaml index ca402fe..cd1aa94 100644 --- a/FFUDevelopment/BuildFFUVM_UI.xaml +++ b/FFUDevelopment/BuildFFUVM_UI.xaml @@ -818,7 +818,7 @@ - + diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 index 13cc782..3e62a0e 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Config.psm1 @@ -114,8 +114,9 @@ function Get-UIConfig { WindowsVersion = $State.Controls.cmbWindowsVersion.SelectedItem } + # Save selected USB drives using UniqueId for reliable identification $State.Controls.lstUSBDrives.Items | Where-Object { $_.IsSelected } | ForEach-Object { - $config.USBDriveList[$_.Model] = $_.SerialNumber + $config.USBDriveList[$_.Model] = $_.UniqueId } # Additional FFU file selections @@ -669,8 +670,9 @@ function Update-UIFromConfig { } } - if ($propertyExists -and ($propertyValue -eq $item.SerialNumber)) { - WriteLog "LoadConfig: Selecting USB Drive Model '$($item.Model)' with Serial '$($item.SerialNumber)'." + # Match USB drives by UniqueId instead of SerialNumber + if ($propertyExists -and ($propertyValue -eq $item.UniqueId)) { + WriteLog "LoadConfig: Selecting USB Drive Model '$($item.Model)' with UniqueId '$($item.UniqueId)'." $item.IsSelected = $true } else { diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 index f18eec3..e43e6b9 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Initialize.psm1 @@ -655,14 +655,14 @@ function Initialize-DynamicUIElements { $modelColumn.Header = $modelHeader } - # Serial Number Column (index 1 in XAML, now 2) + # Unique ID Column (index 1 in XAML, now 2) if ($usbDrivesGridView.Columns.Count -gt 2) { - $serialColumn = $usbDrivesGridView.Columns[2] - $serialHeader = New-Object System.Windows.Controls.GridViewColumnHeader - $serialHeader.Content = "Serial Number" - $serialHeader.Tag = "SerialNumber" # Property to sort by - $serialHeader.HorizontalContentAlignment = [System.Windows.HorizontalAlignment]::Left - $serialColumn.Header = $serialHeader + $uniqueIdColumn = $usbDrivesGridView.Columns[2] + $uniqueIdHeader = New-Object System.Windows.Controls.GridViewColumnHeader + $uniqueIdHeader.Content = "Unique ID" + $uniqueIdHeader.Tag = "UniqueId" # Property to sort by + $uniqueIdHeader.HorizontalContentAlignment = [System.Windows.HorizontalAlignment]::Left + $uniqueIdColumn.Header = $uniqueIdHeader } # Size Column (index 2 in XAML, now 3) diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 index ebc20ed..adffc1b 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.psm1 @@ -184,18 +184,34 @@ function Get-GeneralDefaults { } # Function to get USB Drives (Moved from BuildFFUVM_UI.ps1) +# Uses Get-Disk to retrieve UniqueId which is more reliable than SerialNumber +# UniqueId is trimmed to remove the machine name suffix (characters after colon) function Get-USBDrives { Get-WmiObject Win32_DiskDrive | Where-Object { ($_.MediaType -eq 'Removable Media' -or $_.MediaType -eq 'External hard disk media') } | ForEach-Object { $size = [math]::Round($_.Size / 1GB, 2) - $serialNumber = if ($_.SerialNumber) { $_.SerialNumber.Trim() } else { "N/A" } + # Get the disk using the index to retrieve UniqueId + $disk = Get-Disk -Number $_.Index -ErrorAction SilentlyContinue + # Trim the machine name suffix (everything after the colon) from UniqueId + $uniqueId = if ($disk -and $disk.UniqueId) { + $rawId = $disk.UniqueId + if ($rawId -match ':') { + $rawId.Split(':')[0] + } + else { + $rawId + } + } + else { + "N/A" + } @{ - IsSelected = $false - Model = $_.Model.Trim() - SerialNumber = $serialNumber - Size = $size - DriveIndex = $_.Index + IsSelected = $false + Model = $_.Model.Trim() + UniqueId = $uniqueId + Size = $size + DriveIndex = $_.Index } } }