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.
This commit is contained in:
rbalsleyMSFT
2025-11-24 16:46:57 -08:00
parent 18367219c8
commit 417be73b23
5 changed files with 63 additions and 25 deletions
+28 -8
View File
@@ -3449,18 +3449,38 @@ Function Get-USBDrive {
# Log the count of specified USB drives # Log the count of specified USB drives
$USBDriveListCount = $USBDriveList.Count $USBDriveListCount = $USBDriveList.Count
WriteLog "Looking for $USBDriveListCount USB drives from USB Drive List" 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 = @() $USBDrives = @()
foreach ($model in $USBDriveList.Keys) { foreach ($model in $USBDriveList.Keys) {
$serialNumber = $USBDriveList[$model] $configUniqueId = $USBDriveList[$model]
Writelog "Looking for USB drive model $model with serial number $serialNumber" WriteLog "Looking for USB drive model $model with UniqueId $configUniqueId"
$USBDrive = Get-CimInstance -ClassName Win32_DiskDrive -Filter "Model LIKE '%$model%' AND SerialNumber LIKE '$serialNumber%' AND (MediaType='Removable Media' OR MediaType='External hard disk media')" # First get candidate drives by model and media type
if ($USBDrive) { $candidateDrives = Get-CimInstance -ClassName Win32_DiskDrive -Filter "Model LIKE '%$model%' AND (MediaType='Removable Media' OR MediaType='External hard disk media')"
WriteLog "Found USB drive model $($USBDrive.model) with serial number $($USBDrive.serialNumber)" $foundDrive = $null
$USBDrives += $USBDrive 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 { 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 $USBDrivesCount = $USBDrives.Count
+1 -1
View File
@@ -818,7 +818,7 @@
<GridView> <GridView>
<GridViewColumn Header="Model" DisplayMemberBinding="{Binding Model}" Width="200"/> <GridViewColumn Header="Model" DisplayMemberBinding="{Binding Model}" Width="200"/>
<GridViewColumn Header="Serial Number" DisplayMemberBinding="{Binding SerialNumber}" Width="150"/> <GridViewColumn Header="Unique ID" DisplayMemberBinding="{Binding UniqueId}" Width="300"/>
<GridViewColumn Header="Size (GB)" DisplayMemberBinding="{Binding Size}" Width="80"/> <GridViewColumn Header="Size (GB)" DisplayMemberBinding="{Binding Size}" Width="80"/>
</GridView> </GridView>
</ListView.View> </ListView.View>
@@ -114,8 +114,9 @@ function Get-UIConfig {
WindowsVersion = $State.Controls.cmbWindowsVersion.SelectedItem WindowsVersion = $State.Controls.cmbWindowsVersion.SelectedItem
} }
# Save selected USB drives using UniqueId for reliable identification
$State.Controls.lstUSBDrives.Items | Where-Object { $_.IsSelected } | ForEach-Object { $State.Controls.lstUSBDrives.Items | Where-Object { $_.IsSelected } | ForEach-Object {
$config.USBDriveList[$_.Model] = $_.SerialNumber $config.USBDriveList[$_.Model] = $_.UniqueId
} }
# Additional FFU file selections # Additional FFU file selections
@@ -669,8 +670,9 @@ function Update-UIFromConfig {
} }
} }
if ($propertyExists -and ($propertyValue -eq $item.SerialNumber)) { # Match USB drives by UniqueId instead of SerialNumber
WriteLog "LoadConfig: Selecting USB Drive Model '$($item.Model)' with Serial '$($item.SerialNumber)'." if ($propertyExists -and ($propertyValue -eq $item.UniqueId)) {
WriteLog "LoadConfig: Selecting USB Drive Model '$($item.Model)' with UniqueId '$($item.UniqueId)'."
$item.IsSelected = $true $item.IsSelected = $true
} }
else { else {
@@ -655,14 +655,14 @@ function Initialize-DynamicUIElements {
$modelColumn.Header = $modelHeader $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) { if ($usbDrivesGridView.Columns.Count -gt 2) {
$serialColumn = $usbDrivesGridView.Columns[2] $uniqueIdColumn = $usbDrivesGridView.Columns[2]
$serialHeader = New-Object System.Windows.Controls.GridViewColumnHeader $uniqueIdHeader = New-Object System.Windows.Controls.GridViewColumnHeader
$serialHeader.Content = "Serial Number" $uniqueIdHeader.Content = "Unique ID"
$serialHeader.Tag = "SerialNumber" # Property to sort by $uniqueIdHeader.Tag = "UniqueId" # Property to sort by
$serialHeader.HorizontalContentAlignment = [System.Windows.HorizontalAlignment]::Left $uniqueIdHeader.HorizontalContentAlignment = [System.Windows.HorizontalAlignment]::Left
$serialColumn.Header = $serialHeader $uniqueIdColumn.Header = $uniqueIdHeader
} }
# Size Column (index 2 in XAML, now 3) # Size Column (index 2 in XAML, now 3)
+22 -6
View File
@@ -184,18 +184,34 @@ function Get-GeneralDefaults {
} }
# Function to get USB Drives (Moved from BuildFFUVM_UI.ps1) # 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 { function Get-USBDrives {
Get-WmiObject Win32_DiskDrive | Where-Object { Get-WmiObject Win32_DiskDrive | Where-Object {
($_.MediaType -eq 'Removable Media' -or $_.MediaType -eq 'External hard disk media') ($_.MediaType -eq 'Removable Media' -or $_.MediaType -eq 'External hard disk media')
} | ForEach-Object { } | ForEach-Object {
$size = [math]::Round($_.Size / 1GB, 2) $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 IsSelected = $false
Model = $_.Model.Trim() Model = $_.Model.Trim()
SerialNumber = $serialNumber UniqueId = $uniqueId
Size = $size Size = $size
DriveIndex = $_.Index DriveIndex = $_.Index
} }
} }
} }