powershell - 基于命令行的ProcessID如何匹配

标签 powershell

我想基于进程命令行获取进程ID。
$saucelab = gwmi Win32_Process -Filter "name = 'java.exe'" | select CommandLine, ProcessID
现在可能有许多名称为“java”的进程,但我想查找包含我的字符串的特定进程的进程。
$pid = $saucelab | Where-Object {$_.CommandLine -contains "-port 4444"} | select ProcessID
这行不通

有什么方法可以根据过程processID匹配来获取commandLine

最佳答案

-contains是一个数组运算符。命令行将是一个字符串。尝试改用-match:

$pid = $saucelab | Where-Object {$_.CommandLine -match "-port 4444"} | 
select -ExpandProperty ProcessID

看到:
 Get-Help about_comparison_operators 

编辑:选择一个属性时,将获得一个具有一个属性的对象。如果只需要属性值,请使用-ExpandProperty参数。

关于powershell - 基于命令行的ProcessID如何匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22565350/

相关文章:

powershell - 自动执行PowerShell响应以提示

excel - 如何使用powershell将excel 2007保存为html格式?

powershell - 调用WebRequest和希伯来语字符

powershell - 如何通过管道传输到位于带空格的路径中的 PowerShell 脚本?

PowerShell:如何返回 Hyper-V 群集中的所有 VM

powershell - 将 bool 值转换为字符串

regex - 用powershell中的控制字符Field Seperator(\034)替换一个字符

powershell - PowerShell:如何从定界文本输入中删除列?

powershell - 当前执行的powershell脚本的路径

powershell - 使用混合使用权益运行 New-AzureRmVm 的 Powershell 中出现错误