Powershell发现孤立的进程

我正在寻找一种方法来查找没有父进程运行的进程(孤立进程)。 我试图做到这一点使用win32_process。 我有查询,返回所需的属性,它的比较im挣扎着:

gwmi win32_process -ComputerName $hostname | select ProcessID,ParentProcessID,@{l="Username";e={$_.getowner().user}} | where{$_.Username -like $username} 

我已经尝试了对两个数组的比较对象-includeequal,并得到了绝大多数的结果 – 太多了,所以我怀疑操作员的真相给予我喂它的数组。 我认为在diff命令中增加了一些值,但是除了提供数组之外,我还不熟悉这个用法。 有没有人有差异的命令和/或其他解决scheme的经验?

最终目标是比较或区分上述wmi调用的两个数组:

 $proc_all = gwmi win32_process -ComputerName $hostname | select ProcessID,ParentProcessID,@{l="Username";e={$_.getowner().user}} | where{$_.Username -like $username} $sub_procs = $proc_all.Processid #ARRAY1 $par_proces = $proc_all.ParentProcessId #ARRAY2 

然后只返回那些不出现在两个(孤儿)的。 提前致谢!

可能不是很有效率,但它似乎工作:

 $all_Processes = gwmi win32_process -ComputerName . | select ProcessID,ParentProcessID,Description,@{l="Username";e={$_.getowner().user}} | where{$_.Username -like $env:username} $all_processIDs = $all_Processes.Processid #ARRAY1 $parent_processIDs = $all_Processes.ParentProcessId #ARRAY2 # create a new Array for parents that are gone $gone = New-Object System.Collections.ArrayList # loop through all processes $parent_processIDs | Sort-Object -Unique | ForEach-Object { # find the ones where the current parent ID is not running if ($all_processIDs -notcontains $_) { $gone.Add($_) | Out-Null } } # now we have all parentIDs no longer running # loop through all processes and find those in that list $all_Processes | Where-Object {$gone -contains $_.ParentProcessId} | ForEach-Object {$_}