Last active
March 9, 2024 04:05
-
-
Save hitsmaxft/899bbcd230c1d854ba2fd57af489cc96 to your computer and use it in GitHub Desktop.
usbipd auto connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env powershell.exe | |
# example: .\usbipd-connect.ps1 04d9:8009 | |
# require for powershell https://learn.microsoft.com/en-us/windows/sudo/ | |
# auto gen by chatgpt4 | |
param([string]$vid_pid) | |
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
[Console]::InputEncoding = [System.Text.Encoding]::UTF8 | |
while ($true) { | |
# STEP0 | |
$usbipdListOutput = usbipd.exe list | |
# Find the line number where "Persisted:" starts | |
$lineOfPersisted = ($usbipdListOutput | Select-String "Persisted:" -AllMatches).LineNumber | |
# Get all lines before "Persisted:" | |
$relevantOutputLines = $usbipdListOutput -split "`n" | Select-Object -First ($lineOfPersisted - 1) | |
# Join the lines back to a single string to continue with the existing processing | |
$relevantOutput = $relevantOutputLines -join "`n" | |
# Write-Host $relevantOutput | |
# Check if the VID:PID exists in the list | |
$deviceLine = ($relevantOutput -split "\n") | Where-Object { $_ -match "$vid_pid" } | |
if (-not $deviceLine) { | |
# STEP3 - Report error | |
Write-Host "device not found" | |
Start-Sleep -Seconds 3 | |
continue | |
} | |
# Parsing the variables | |
$parts = $deviceLine -split "\s{2,}" | |
# foreach ($part in $parts) { | |
# Write-Output "["$part"]" | |
# } | |
$busId = $parts[0] | |
$vidPid = $parts[1] | |
$device = $parts[2] | |
$state = $parts[3].TrimStart() | |
# Check the status of the device | |
switch ($state) { | |
"Shared" { | |
# STEP1 - Device is shared, try to attach | |
try { | |
Write-Host "Connecting" | |
sudo usbipd.exe wsl attach --busid $busId | |
Start-Sleep -Seconds 1 | |
} catch { | |
Write-Host "An error occurred while trying to attach the device." | |
} | |
} | |
"Attached" { | |
# STEP2 - Device is attached, sleep for 5s | |
Start-Sleep -Seconds 5 | |
} | |
default { | |
Write-Host "status unknown:$state" | |
Start-Sleep -Seconds 3 | |
} | |
} | |
Start-Sleep -Seconds 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment