From 78f7abf82ad42bdadaa03ccf606051d2c01a1821 Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Thu, 20 Feb 2025 08:45:50 -0800 Subject: [PATCH] Optimizations in Devices --- Get-PC/Private/Get-Devices.ps1 | 76 ++++++++++++++-------------------- Get-PC/patchnotes.txt | 3 ++ 2 files changed, 33 insertions(+), 46 deletions(-) diff --git a/Get-PC/Private/Get-Devices.ps1 b/Get-PC/Private/Get-Devices.ps1 index 2b4b84f..ff208ec 100644 --- a/Get-PC/Private/Get-Devices.ps1 +++ b/Get-PC/Private/Get-Devices.ps1 @@ -33,15 +33,7 @@ function Get-Devices { } } Write-Progress -Activity "Getting devices for $name" -Status 'Sorting out duplicate devices' -PercentComplete 90 -ParentId 1 - $usb = $usb | Sort-Object Manufacturer,Description - for ($i = 0; $i -lt $usb.Count - 1; $i++) { - if($usb[$i].Manufacturer -eq $usb[$i+1].Manufacturer -and $usb[$i].Description -eq $usb[$i+1].Description){ - $usb[$i].Manufacturer = '__DUPE__' - } - } - $usb = $usb | Where-Object -Property Manufacturer -NE '__DUPE__' - $usb = $usb | Sort-Object Type,Manufacturer - + $usb = $usb | Sort-Object Manufacturer,Description -Unique | Sort-Object Type,Manufacturer } catch{ Write-warning 'Unable to grab USB info' @@ -298,52 +290,44 @@ function FindUSBDevice { [string]$deviceID ) begin { - # Start with SHS Equipment override - $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\SHSEquipmentLookup.txt' - $usbIDs = Get-Content $lookupTablePath $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\USBIDs.txt' $usbIDs += Get-Content $lookupTablePath + # Overwrite any USBIDs with SHS Overrides + $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\SHSEquipmentLookup.txt' + $usbIDs = Get-Content $lookupTablePath + + # Populate lookup table from file contents + $usbIDTable = @{} + foreach ($line in $usbIDs) { + # Skip comments + if ($line -match "^#") { continue } + if ($line -match "^[\t]([0-9a-f]{4})\s+([^#]+)") { + $usbIDTable[$vid]['Devices'][$Matches[1]] = $Matches[2] + continue + # If line doesn't match device id definition, see if it's a vendor definition + } else { + if (-not ($line -match "^([0-9a-f]{4})\s+([^#]+)")) { continue } + $usbIDTable[$Matches[1]] = @{ + Name = $Matches[2] + Devices = @{} + } + $vid = $Matches[1] + continue + } + } } process { if($deviceID){ - $bool = $deviceID -match '(?<=VID_)(....)' - if(!$bool){ - return $null - } + if (-not( $deviceID -match '(?<=VID_)(....)' )) { return $null } $vid = $Matches[0] $deviceID -match '(?<=PID_)(....)' | Out-Null $p = $Matches[0] } - for ($i = 0; $i -lt $usbIDs.Count; $i++) { - if (-not ($usbIDs[$i] -match $vid)){ continue } - - if($usbIDs[$i][0] -eq "`t"){ continue } - - $man = ($usbIDs[$i] -split ' ') - $man = $man[2..$man.count] -join ' ' - - for ($y = $i+1; $y -lt $usbIDs.Count; $y++) { - if($usbIDs[$y][0] -ne "`t"){ - return @{ - Manufacturer = $man - Description = $null - } - } - if($usbIDs[$y] -match $p){ - $dev = ($usbIDs[$y] -split ' ') - $dev = $dev[2..$dev.count] -join ' ' - return @{ - Manufacturer = $man - Description = $dev - } - } - - } - return @{ - Manufacturer = $man - Description = $null - } + if (-not $usbIDTable[$vid]) { return $null } + return @{ + Manufacturer = $usbIDTable[$vid]['Name'] + Description = $usbIDTable[$vid]['Devices'][$p] } } end { } -} +} \ No newline at end of file diff --git a/Get-PC/patchnotes.txt b/Get-PC/patchnotes.txt index a91fafb..81de4b4 100644 --- a/Get-PC/patchnotes.txt +++ b/Get-PC/patchnotes.txt @@ -1,3 +1,6 @@ +Patch +-Fix Devices and DevicesUnplugged 1.4x speedup + Patch 2025-2-13 -Add HostnamesByPrinter to list hostnames with printer installed -Add JobsPrinters to query many printers faster