英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论