Improves USB drive selection for same-model drives

Preserves multiple selected drives that share the same model by storing an array of UniqueIds per model.

Updates drive discovery and UI restore logic to accept either a single UniqueId or a list, preventing missed selections and skipping duplicate additions.
This commit is contained in:
rbalsleyMSFT
2026-02-03 13:37:58 -08:00
parent 1feed40962
commit ed0266029a
2 changed files with 82 additions and 32 deletions
+23 -4
View File
@@ -195,9 +195,11 @@ Path to a JSON file containing a list of user-defined applications to install. D
.PARAMETER USBDriveList
A hashtable containing USB drives from win32_diskdrive where:
- Key: USB drive model name (partial match supported)
- Value: USB drive serial number (trailing partial match supported due to some serial numbers ending with blank spaces)
- Value: USB drive UniqueId string, or an array of UniqueIds (to support selecting multiple drives with the same model)
Example: @{ "SanDisk Ultra" = "1234567890"; "Kingston DataTraveler" = "0987654321" }
Examples:
@{ "SanDisk Ultra" = "1234567890" }
@{ "SanDisk Ultra" = @("1234567890", "ABCDEFG"); "Kingston DataTraveler" = "0987654321" }
.PARAMETER MaxUSBDrives
Maximum number of USB drives to build in parallel. Default is 5. Set to 0 to process all discovered drives (or all selected drives when USBDriveList or selection is used). Actual throttle will never exceed the number of drives discovered.
@@ -3961,12 +3963,23 @@ Function Get-USBDrive {
}
elseif ($USBDriveList) {
# Log the count of specified USB drives
$USBDriveListCount = $USBDriveList.Count
# USBDriveList values can be a single UniqueId string, or an array of UniqueIds (multiple same-model drives)
$USBDriveListCount = 0
foreach ($model in $USBDriveList.Keys) {
$USBDriveListCount += @($USBDriveList[$model]).Count
}
WriteLog "Looking for $USBDriveListCount USB drives from USB Drive List"
# Get only the specified USB drives based on model and UniqueId
$USBDrives = @()
foreach ($model in $USBDriveList.Keys) {
$configUniqueId = $USBDriveList[$model]
$configUniqueIds = @($USBDriveList[$model])
foreach ($configUniqueId in $configUniqueIds) {
if ([string]::IsNullOrWhiteSpace([string]$configUniqueId)) {
continue
}
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')"
@@ -3990,13 +4003,19 @@ Function Get-USBDrive {
}
}
if ($foundDrive) {
if ($USBDrives.Index -notcontains $foundDrive.Index) {
WriteLog "Found USB drive model $($foundDrive.Model) with UniqueId $configUniqueId"
$USBDrives += $foundDrive
}
else {
WriteLog "USB drive model $($foundDrive.Model) with UniqueId $configUniqueId was already added. Skipping duplicate."
}
}
else {
WriteLog "USB drive model $model with UniqueId $configUniqueId not found"
}
}
}
$USBDrivesCount = $USBDrives.Count
WriteLog "Found $USBDrivesCount of $USBDriveListCount USB drives from USB Drive List"
}
@@ -115,8 +115,27 @@ function Get-UIConfig {
}
# Save selected USB drives using UniqueId for reliable identification
# Multiple physical drives can share the same Model, so store an array of UniqueIds per Model.
$State.Controls.lstUSBDrives.Items | Where-Object { $_.IsSelected } | ForEach-Object {
$config.USBDriveList[$_.Model] = $_.UniqueId
$modelName = $_.Model
$uniqueId = $_.UniqueId
if ([string]::IsNullOrWhiteSpace($modelName) -or [string]::IsNullOrWhiteSpace($uniqueId)) {
return
}
# Ensure the hashtable value is always an array so multiple same-model drives are preserved
$existingUniqueIds = $config.USBDriveList[$modelName]
if ($null -eq $existingUniqueIds) {
$config.USBDriveList[$modelName] = @($uniqueId)
return
}
$existingUniqueIds = @($existingUniqueIds)
if (-not ($existingUniqueIds -contains $uniqueId)) {
$existingUniqueIds += $uniqueId
}
$config.USBDriveList[$modelName] = $existingUniqueIds
}
# Additional FFU file selections
@@ -671,7 +690,19 @@ function Update-UIFromConfig {
}
# Match USB drives by UniqueId instead of SerialNumber
if ($propertyExists -and ($propertyValue -eq $item.UniqueId)) {
# USBDriveList values can be a single UniqueId (string) or an array of UniqueIds (multiple same-model drives)
$isMatch = $false
if ($propertyExists) {
if ($propertyValue -is [string]) {
$isMatch = ($propertyValue -eq $item.UniqueId)
}
else {
$propertyValueArray = @($propertyValue)
$isMatch = ($propertyValueArray -contains $item.UniqueId)
}
}
if ($isMatch) {
WriteLog "LoadConfig: Selecting USB Drive Model '$($item.Model)' with UniqueId '$($item.UniqueId)'."
$item.IsSelected = $true
}