如何通过命令行参数停止进程?

How to Stop-Process by command line parameters?

我正在尝试自动化一些东西,我需要能够 Stop-Process 通过 dotnet path\to\myProgram.dll 启动的应用程序。

我试过了Stop-Process -Name myProgram -PassThru,但它说找不到任何类似的进程。
只是做 Stop-Process -Name dotnet -PassThru 似乎有点喷雾'n'祈祷,因为有几个 DotNet 程序 运行 我想停止一个特定的程序。

如何找到正确的过程以提供给 Stop-Process

您可以获得进程 运行 特定的命令行并停止它。

下面的例子:

$Process = Get-CimInstance -ClassName Win32_PRocess -Filter "CommandLine='C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe'"

$Process = Get-CimInstance -ClassName Win32_PRocess -Filter "CommandLine LIKE '%powershell.exe'"

然后

$Process | Invoke-WmiMethod -Name Terminate

识别特定 dotnet 进程使用的命令行并使用该 cmdline 对其进行过滤。