Powershell如何通过ProcessID获取ParentProcessID

Pas*_*ann 4 powershell

我有问题从我拥有ProcessID的Process获取ParentProcessID.我这样尝试过,这就是它对ProcessID的作用:

$p = Get-Process firefox
$p.Id
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用ParentProcessID尝试它,它不起作用:

$p.ParentProcessId
Run Code Online (Sandbox Code Playgroud)

有没有办法通过ProcessID获取ParentProcessID?

Pas*_*ann 10

这对我有用:

$p = Get-Process firefox
$parent = (gwmi win32_process | ? processid -eq  $p.Id).parentprocessid
$parent
Run Code Online (Sandbox Code Playgroud)

输出如下:

1596
Run Code Online (Sandbox Code Playgroud)

而且1596是匹配ParentProcessID我受够了ProcessExplorer检查它。


Aeg*_*gis 9

在 PowerShell Core 中,cmdletProcess返回的对象Get-Process包含一个 Parent 属性,该属性为您Process提供父进程的相应对象。

例子:

> $p = Get-Process firefox
> $p.Parent.Id
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这需要(提升的)管理员权限。否则它会默默地失败,并且“Parent”成员将为“$null”。[WMI 解决方案](/sf/answers/2373853401/) 使用较少的权限。 (2认同)
  • @zett42我认为这不再是真的,它在没有管理权限的PowerShell上为我工作。 (2认同)

Mat*_*sen 8

如注释中所述,从Get-Process(System.Diagnostics.Process)返回的对象不包含父进程ID.

为此,您需要检索Win32_Process类的实例:

PS C:\> $ParentProcessIds = Get-CimInstance -Class Win32_Process -Filter "Name = 'firefox.exe'"
PS C:\> $ParentProcessIds[0].ParentProcessId
3816
Run Code Online (Sandbox Code Playgroud)