function Get-DevicesUnplugged {
    param (
        [string]$comp
    )
    Write-Progress -Activity "Getting devices for $name" -Status 'Collecting USB devices' -PercentComplete 10 -ParentId 1
    if ($comp -eq $env:COMPUTERNAME) {
        $usb += Get-PnpDevice -Status UNKNOWN | Select-Object @{N="Description";E={$_.FriendlyName}},@{N="DeviceID\SerialNumber";E={$_.InstanceID}} | Add-Member -NotePropertyName Manufacturer -NotePropertyValue 'Unknown' -PassThru
    } else {
        $usb += Invoke-Command -ComputerName $comp -SessionOption $(New-PSSessionOption -NoMachineProfile) -ScriptBlock {
            Get-PnpDevice -Status UNKNOWN | Select-Object @{N="Description";E={$_.FriendlyName}},@{N="DeviceID\SerialNumber";E={$_.InstanceID}} | Add-Member -NotePropertyName Manufacturer -NotePropertyValue 'Unknown' -PassThru
        }
    }
    $usb | Add-Member -NotePropertyName Type -NotePropertyValue USB
    $usb | Add-Member -NotePropertyName AttachedComputer -NotePropertyValue $comp
    $usbi = 0
    foreach($item in $usb){
        $usbi++
        Write-Progress -Activity "Getting devices for $name" -Status "Parsing USB Devices ($usbi/$($usb.Length)" -PercentComplete (($usbi * (90-10) /  $usb.Length )+10) -ParentId 1
        $friendlyNames = FindUSBDevice -deviceID $item.'DeviceID\SerialNumber'.ToString()
        
        if($friendlyNames){
            $item.Manufacturer = $friendlyNames[0]
            if($item.Manufacturer.Length -le 5){
                continue
            }
            if($item.Manufacturer.Substring(0,5) -eq '[SHS]'){
                $item.Type = 'SHS Issued USB'
                $len = $item.Manufacturer.Length -1
                $item.Manufacturer = $item.Manufacturer[5..$len] -join ''
            }
            if($friendlyNames[1]){
                $item.Description = $friendlyNames[1]
            }
        }
    }
    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
    
    $out = @()

    foreach($item in ($monitors + $usb)){
        $item.PSObject.TypeNames.Insert(0,'GetPC.Devices')
        $out += $item   
    }
    $item = [PSCustomObject]@{ }
    $item.PSObject.TypeNames.Insert(0,'GetPC.Devices')
    $out += $item
    
    Write-Output $out
}


function FindUSBDevice {
    param (
        [string]$vid,
        [string]$p,
        [string]$deviceID
    )
    if($deviceID){
        $bool = $deviceID -match '(?<=VID_)(....)'
        if(!$bool){
            return $null
        }
        $vid = $Matches[0]
        $deviceID -match '(?<=PID_)(....)' | Out-Null
        $p = $Matches[0]
    }
    $SHSEquip = MatchSHSEquipment $vid $p
    if($SHSEquip){
        return ($SHSEquip[0],$SHSEquip[1])
    }
    $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\USBIDs.txt'
    $usbIDs = Get-Content $lookupTablePath
    for ($i = 0; $i -lt $usbIDs.Count; $i++) {
        if ($usbIDs[$i] -match $vid){
            
            if($usbIDs[$i][0] -eq "`t"){
                
            }
            else{
                
                $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 ($man,$null)
                    }
                    if($usbIDs[$y] -match $p){
                        $dev = ($usbIDs[$y] -split ' ')
                        $dev = $dev[2..$dev.count] -join ' '
                        return ($man,$dev)
                    }
        
                }
                return ($man,$null)
            }
            
        }
        
    
    }  
}

function MatchSHSEquipment {
    param (
        $vid,
        $p
    )
    $lookupTablePath = Join-Path (get-item $PSScriptRoot).Parent.FullName 'Data\SHSEquipmentLookup.txt'
    $usbIDs = Get-Content $lookupTablePath
    for ($i = 0; $i -lt $usbIDs.Count; $i++) {
        if ($usbIDs[$i] -match $vid){
            
            if($usbIDs[$i][0] -eq "`t"){
                
            }
            else{
                
                $man = ($usbIDs[$i] -split ' ')
                $man = $man[2..$man.count] -join ' '

                for ($y = $i; $y -lt $usbIDs.Count; $y++) {
                    if($usbIDs[$y] -match $p){
                        $dev = ($usbIDs[$y] -split ' ')
                        $dev = $dev[2..$dev.count] -join ' '
                        return ($man,$dev)
                    }
        
                }
                return $null
            }
            
        }
        
    
    }
    
    return $null
}
