PowerShell 终止通过其命令行检测到的 Java 进程

huangapple go评论85阅读模式
英文:

PowerShell terminate java process detected by its commandline

问题

我正在启动一个Java进程:

$global:TS = Start-Process -FilePath "$pathTS\jdk\bin\java.exe" -ArgumentList "-jar","`"$pathTS\TestServer.jar`"" -WorkingDirectory "$pathTS" -PassThru -WindowStyle Hidden

目前工作正常。

现在我正在尝试终止该进程:

Invoke-CimMethod -Query 'select * from Win32_Process where CommandLine like "Test-Server"' -MethodName "Terminate"

顺便说一下:我不能使用进程的ID来终止它,因为它在某种程度上在变化。

我对PowerShell不熟,现在陷入困境。
也许有人有想法。

谢谢!
Alex

英文:

I am starting a java process:

$global:TS = Start-Process -FilePath "$pathTS\jdk\bin\java.exe" -ArgumentList "-jar","`"$pathTS\TestServer.jar`"" -WorkingDirectory ` "$pathTS" ` -PassThru -WindowStyle Hidden

works fine so far.

Now I am trying to terminate that process:

 Invoke-CimMethod -Query 'select * from Win32_Process where CommandLine like "Test-Server"' -MethodName "Terminate"

BTW: I cannot use the ID of the process to terminate it since it is changing somehow.

I am new to PS and now stuck.
Maybe someone has in idea.

Thank you!
Alex

答案1

得分: 1

尝试在WHERE子句中添加通配符字符%,以查找所有实例中在CommandLine中任何位置找到“Test-Server”的情况:

... WHERE CommandLine like "%Test-Server%"...

参考链接:https://learn.microsoft.com/en-us/windows/win32/wmisdk/like-operator

英文:

Try to add the wildcard character % to the WHERE clause to find all instances where "Test-Server" is found anywhere in the CommandLine :

... WHERE CommandLine like "%Test-Server%"...

ref : https://learn.microsoft.com/en-us/windows/win32/wmisdk/like-operator

huangapple
  • 本文由 发表于 2020年9月11日 00:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63833740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定