mirror of
https://github.com/rbalsleyMSFT/FFU.git
synced 2026-06-14 02:09:35 -06:00
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:
@@ -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 {
|
||||
WriteLog "USB drive model $model with serial number $serialNumber not found"
|
||||
$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 UniqueId $configUniqueId not found"
|
||||
}
|
||||
}
|
||||
$USBDrivesCount = $USBDrives.Count
|
||||
|
||||
@@ -818,7 +818,7 @@
|
||||
<GridView>
|
||||
|
||||
<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"/>
|
||||
</GridView>
|
||||
</ListView.View>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -184,16 +184,32 @@ 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
|
||||
UniqueId = $uniqueId
|
||||
Size = $size
|
||||
DriveIndex = $_.Index
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user