gpt4 book ai didi

powershell,如何仅给定父进程ID来获取子进程ID

转载 作者:行者123 更新时间:2023-12-05 02:36:48 27 4
gpt4 key购买 nike

假设我像这样启动一个 powershell 进程:

    $procid = start-process -FilePath powershell _
-ArgumentList ping, -t, localhost

如何仅给定 powershell 的进程 ID 来获取“ping”的进程 ID,即。 $procid?

因为,我的脚本里只有$procid,需要找到子进程的procid。

enter image description here

这里可以看到powershell的pid是3328,我需要用3328查询powershell找到id:7236(Ping.exe)。

最佳答案

cudo 到 mklement0 和 nordmanden

您可以使用 CIM cmdlet 过滤给定进程的 ParentProcessId,并在递归函数中使用它来获取整个树

function Get-ChildProcesses ($ParentProcessId) {
$filter = "parentprocessid = '$($ParentProcessId)'"
Get-CIMInstance -ClassName win32_process -filter $filter | Foreach-Object {
$_
if ($_.ParentProcessId -ne $_.ProcessId) {
Get-ChildProcesses $_.ProcessId
}
}
}

这样调用

Get-ChildProcesses 4 | Select ProcessId, Name, ParentProcessId

请注意,进程可以终止(由用户、崩溃、完成...)并且 ID 可以被回收。从理论上讲,您最终可以得到所有将记事本作为父进程的进程树。

关于powershell,如何仅给定父进程ID来获取子进程ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70179457/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com