powershell - 如何在powershell中获取进程的 "Comand Line"属性

标签 powershell

我想知道如何获取任务管理器中显示的进程的“命令行”属性。我的意思是,如果我运行以下命令

Get-Process -Name "Firefox" | ? {$_.TotalProcessorTime -ne $Null} | Select-Object -Property Name, Id, Path

我明白了:

Name       Id Path                                         
----       -- ----                                         
firefox   728 C:\Program Files\Mozilla Firefox\firefox.exe   
firefox  2260 C:\Program Files\Mozilla Firefox\firefox.exe  
firefox  2612 C:\Program Files\Mozilla Firefox\firefox.exe    
firefox  3992 C:\Program Files\Mozilla Firefox\firefox.exe   

但我不需要“Path”属性,而是需要命令行,我的意思是例如:

Name   Id    **Command Line**                                                             
----   --  ----                                                                      
firefox 728 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.3.14958**                               
firefox 2260 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.4.9583**                               
firefox 2612 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.5.1392**                                
firefox 3992 **"C:\Program Files\Mozilla Firefox\firefox.exe" -contentproc --chanel="1306.6.21397**                                

稍后对字符串的一部分进行过滤(执行 grep),找到命令行列(例如:1306.5.1392)我知道我可以使用 findstr -i "1306.5.1392",但不确定是否可以这是最聪明的方法

非常感谢您的帮助!

亲切的问候

最佳答案

查询the Win32_Process WMI class的实例获取命令行:

Get-CimInstance -ClassName Win32_Process -Filter "Name = 'firefox.exe'" |Where-Object { 
    (Get-Process -Id $_.ProcessId).TotalProcessorTime -ne $null 
} |Select Name,ProcessId,CommandLine

如果您想在输出中包含 TotalProcessorTime 值,请在过滤之前使用 Select-Object:

Get-CimInstance -ClassName Win32_Process -Filter "Name = 'firefox.exe'" |Select-Object Name,ProcessId,CommandLine,@{Name='TotalProcessorTime';Expression={(Get-Process -Id $_.ProcessId).TotalProcessorTime}} |Where-Object { $_.TotalProcessorTime -eq $null }

关于powershell - 如何在powershell中获取进程的 "Comand Line"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73988459/

相关文章:

azure - 从 PowerShell 部署 LogicApp

powershell - 在powershell中获取blob存储内容

class - 如何在 Powershell 5 类中声明静态成员?

windows - 如何在 PowerShell 中比较两个日期并以分钟为单位计算差异

xml - 如何使用 Powershell 从 XML 文件中删除特定标签?

powershell - 如何使用 Powershell 将 .Net Web 应用程序部署到 Azure

xml - 如何使用 Powershell 枚举 XML 元素的属性

sql-server-2005 - 我可以将 PowerShell 与 SQL Server 2005 一起使用吗?

Powershell:如何处理分组对象的值?

powershell - 我的切换功能出了什么问题