|
# Ensure you run PowerShell with admin |
|
|
|
function Get-UsbDevices { |
|
$output = & "C:\Program Files\usbipd-win\usbipd.exe" list |
|
$devices = @() |
|
|
|
$lines = $output -split "`n" |
|
for ($i = 2; $i -lt $lines.Length; $i++) { |
|
$parts = $lines[$i] -split '\s+' |
|
if ($parts.Length -ge 3) { |
|
$devices += [PSCustomObject]@{ |
|
BusId = $parts[0] |
|
Description = $parts[2..($parts.Length - 1)] -join ' ' |
|
State = $parts[1] |
|
} |
|
} |
|
} |
|
return $devices |
|
} |
|
|
|
function Attach-UsbDevice { |
|
param ( |
|
[string]$busid |
|
) |
|
& "C:\Program Files\usbipd-win\usbipd.exe" bind --busid $busid |
|
& "C:\Program Files\usbipd-win\usbipd.exe" attach --wsl --busid $busid |
|
} |
|
|
|
function Detach-UsbDevice { |
|
param ( |
|
[string]$busid |
|
) |
|
& "C:\Program Files\usbipd-win\usbipd.exe" detach --busid $busid |
|
} |
|
|
|
function Monitor-Device { |
|
param ( |
|
[string]$busid |
|
) |
|
while ($true) { |
|
Start-Sleep -Seconds 1 |
|
$stateJson = & "C:\Program Files\usbipd-win\usbipd.exe" state |
|
$device = ($stateJson | ConvertFrom-Json).Devices | Where-Object { $_.BusId -eq $busid } |
|
|
|
if ($device.ClientIPAddress -eq $null) { |
|
Write-Host "Reattaching device $($device.Description)..." |
|
Attach-UsbDevice $busid |
|
} |
|
} |
|
} |
|
|
|
function Show-Menu { |
|
param ( |
|
[array]$devices, |
|
[array]$activeDevices, |
|
[int]$selectedIndex |
|
) |
|
|
|
Clear-Host |
|
Write-Host "Select USB Device (Press Enter to toggle, Q to quit):" |
|
for ($i = 0; $i -lt $devices.Count; $i++) { |
|
if ($i -eq $selectedIndex) { |
|
Write-Host "-> $($devices[$i].BusId) - $($devices[$i].Description) ($($devices[$i].State))" |
|
} else { |
|
Write-Host " $($devices[$i].BusId) - $($devices[$i].Description) ($($devices[$i].State))" |
|
} |
|
} |
|
|
|
Write-Host "`nActive Devices:" |
|
foreach ($device in $activeDevices) { |
|
Write-Host " $($device.BusId) - $($device.Description)" |
|
} |
|
} |
|
|
|
function Main { |
|
$devices = Get-UsbDevices |
|
$activeDevices = @() |
|
$selectedIndex = 0 |
|
|
|
while ($true) { |
|
Show-Menu -devices $devices -activeDevices $activeDevices -selectedIndex $selectedIndex |
|
|
|
$key = [Console]::ReadKey($true) |
|
|
|
if ($key.Key -eq 'UpArrow') { |
|
$selectedIndex = ($selectedIndex - 1 + $devices.Count) % $devices.Count |
|
} elseif ($key.Key -eq 'DownArrow') { |
|
$selectedIndex = ($selectedIndex + 1) % $devices.Count |
|
} elseif ($key.Key -eq 'Enter') { |
|
$selectedDevice = $devices[$selectedIndex] |
|
if ($activeDevices -contains $selectedDevice) { |
|
# Deactivate device |
|
Detach-UsbDevice $selectedDevice.BusId |
|
$activeDevices = $activeDevices | Where-Object { $_ -ne $selectedDevice } |
|
} else { |
|
# Activate device |
|
Attach-UsbDevice $selectedDevice.BusId |
|
$activeDevices += $selectedDevice |
|
} |
|
} elseif ($key.Key -eq 'Q') { |
|
break |
|
} |
|
} |
|
|
|
# Deactivate all active devices before exiting |
|
foreach ($device in $activeDevices) { |
|
Detach-UsbDevice $device.BusId |
|
} |
|
} |
|
|
|
# Start |
|
Main |