Abh*_*raj 3 windows powershell batch-file batch windows-command-prompt
我想列出并终止属于使用端口的特定进程的会话的所有进程。这应该通过 Windows 批处理命令来实现,该命令接受端口号作为输入。
例如:假设进程 PA 当前正在侦听端口 8081。PA 运行在会话 S1 下。有进程 PB 和 PC 与 PA 属于同一会话。PB和PC将运行在不同的端口上(它们运行在哪个端口上并不重要)
Windows 命令/批处理文件应以 8081 作为输入并终止进程 PA、PB 和 PC。
这可能吗?感谢对此的一些帮助,因为我不太熟悉批处理命令/脚本。
我失败的尝试:
(for /F "tokens=2" %%i in (for /f "tokens=5" %a in ('netstat -aon ^| findstr 8081') do tasklist /NH /FI "PID eq %a") do taskkill /NH /FI "SESSIONNAME eq %%i")
Run Code Online (Sandbox Code Playgroud)
这在 PowerShell 中实际上非常简单:
# Get the process of the listening NetTCPConnection and the session ID of the process
$SessionId = (Get-Process -Id (Get-NetTCPConnection -State Listen -LocalPort 8081).OwningProcess).SessionId
# Get all processes from that session and stop them
Get-Process | Where-Object { $_.SessionId -eq $SessionId } |
Stop-Process -Force -Confirm:$false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2493 次 |
| 最近记录: |