#Requires -Version 5.1 <# .SYNOPSIS Diagnose and repair SEGGER J-Link USB connection on Windows. .NOTES Run PowerShell as Administrator for driver reinstall / phantom device removal. Close IAR and J-Link Commander before running. #> $ErrorActionPreference = "Continue" $JLinkExe = "C:\Program Files\SEGGER\JLink_V818\JLink.exe" $DriverInf = "C:\Program Files\SEGGER\JLink_V818\USBDriver\x64\JLink.inf" function Write-Step($text) { Write-Host "`n=== $text ===" -ForegroundColor Cyan } Write-Step "1. J-Link process check" $procs = Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.ProcessName -match '^(JLink|IarIdePm|cspybat|IAR)$' } if ($procs) { Write-Host "[WARN] These may lock J-Link:" -ForegroundColor Yellow $procs | Format-Table ProcessName, Id -AutoSize Write-Host "Close IAR / J-Link Commander windows, then run:" -ForegroundColor Yellow Write-Host ' Stop-Process -Name JLink,IarIdePm -Force -ErrorAction SilentlyContinue' -ForegroundColor Gray } else { Write-Host "[OK] No J-Link / IAR debugger processes" -ForegroundColor Green } Write-Step "2. USB device status (VID 1366 = SEGGER)" $all = Get-PnpDevice -ErrorAction SilentlyContinue | Where-Object { $_.InstanceId -match 'VID_1366' } if (-not $all) { Write-Host "[FAIL] No J-Link entries in PnP (not plugged in or bad cable/port)" -ForegroundColor Red } else { $all | Format-Table FriendlyName, Status, Present, Problem -AutoSize $present = $all | Where-Object { $_.Present -eq $true } if (-not $present) { Write-Host "[FAIL] J-Link entries exist but Present=False (ghost device, Code 45)" -ForegroundColor Red Write-Host " Unplug USB, uninstall hidden 'J-Link driver', replug." -ForegroundColor Yellow } else { Write-Host "[OK] J-Link is present on USB" -ForegroundColor Green } } Write-Step "3. Installed SEGGER drivers" driverquery | Select-String -Pattern 'jlink' -CaseSensitive:$false Write-Step "4. J-Link Commander probe test" if (Test-Path $JLinkExe) { $out = cmd /c "echo exit| `"$JLinkExe`"" 2>&1 | Out-String if ($out -match 'O\.K\.|Found .* J-Link') { Write-Host "[OK] $out.Trim()" -ForegroundColor Green } else { Write-Host "[FAIL] $out.Trim()" -ForegroundColor Red } } else { Write-Host "[MISS] $JLinkExe" -ForegroundColor Red } Write-Step "5. Repair actions (Administrator)" $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "[INFO] Re-run this script as Administrator to auto-repair drivers." -ForegroundColor Yellow Write-Host " Right-click PowerShell -> Run as administrator" -ForegroundColor Yellow exit 0 } Write-Host "Stopping J-Link / IAR helper processes..." Stop-Process -Name JLink -Force -ErrorAction SilentlyContinue Stop-Process -Name IarIdePm -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 Write-Host "Removing phantom J-Link PnP devices..." Get-PnpDevice -ErrorAction SilentlyContinue | Where-Object { $_.InstanceId -match 'VID_1366' } | ForEach-Object { Write-Host " Removing $($_.InstanceId)" pnputil /remove-device $_.InstanceId 2>&1 | Out-Null } if (Test-Path $DriverInf) { Write-Host "Reinstalling J-Link USB driver from JLink_V818..." pnputil /add-driver $DriverInf /install 2>&1 Write-Host "[DONE] Now unplug J-Link USB, wait 5s, plug back in." -ForegroundColor Green Write-Host " Then verify: echo exit | `"$JLinkExe`"" -ForegroundColor Gray } else { Write-Host "[MISS] Driver INF: $DriverInf" -ForegroundColor Red }