kill-all-process.ps1 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Stops adb.exe, node.exe, and Python runtimes. With -OnlyKillProcessesUnderPath, only processes whose Path is under that folder (safe from Electron exit).
  2. param(
  3. [int[]] $ExcludeProcessIds = @(),
  4. [string] $OnlyKillProcessesUnderPath = ''
  5. )
  6. $ErrorActionPreference = 'SilentlyContinue'
  7. # True when node.exe belongs to Cursor install (Local\Programs\cursor or ...\cursor\resources\...).
  8. function Test-IsCursorEditorNode {
  9. param([string]$ProcPath)
  10. if (-not $ProcPath) {
  11. return $false
  12. }
  13. $normalized = $ProcPath.Replace('/', '\')
  14. if ($normalized -match '(?i)\\Programs\\cursor\\') {
  15. return $true
  16. }
  17. if ($normalized -match '(?i)\\cursor\\resources\\') {
  18. return $true
  19. }
  20. return $false
  21. }
  22. function Test-ShouldStopProcess {
  23. param($Proc)
  24. if ($ExcludeProcessIds -contains $Proc.Id) {
  25. return $false
  26. }
  27. if ($OnlyKillProcessesUnderPath -eq '') {
  28. return $true
  29. }
  30. $procPath = $Proc.Path
  31. if (-not $procPath) {
  32. return $false
  33. }
  34. $root = [System.IO.Path]::GetFullPath($OnlyKillProcessesUnderPath)
  35. $full = [System.IO.Path]::GetFullPath($procPath)
  36. return $full.StartsWith($root, [StringComparison]::OrdinalIgnoreCase)
  37. }
  38. function Stop-DevSidecarProcesses {
  39. $baseNames = @('adb', 'node', 'python', 'pythonw', 'python3')
  40. foreach ($baseName in $baseNames) {
  41. Get-Process -Name $baseName -ErrorAction SilentlyContinue |
  42. Where-Object {
  43. if ($baseName -eq 'node' -and (Test-IsCursorEditorNode -ProcPath $_.Path)) {
  44. return $false
  45. }
  46. Test-ShouldStopProcess -Proc $_
  47. } |
  48. Stop-Process -Force
  49. }
  50. }
  51. Stop-DevSidecarProcesses
  52. Write-Host '[OK] adb / node / python cleanup finished.'