我需要编写一个脚本,它与 Windows 中的 运行 服务和进程相关联。将结果放入哈希 table

I need to write a script that it makes association with running services and process in Windows. Put result in hash table

我的脚本与 Windows 中的 运行 服务和进程相关联:

Get-WmiObject Win32_Service | Foreach-Object {
$_ | Add-Member -MemberType NoteProperty -Name Processes -Value (Get-Process -Id $_.ProcessId ) -Passthru
   | Select-Object ProcessId, Name, State, Processes | Where-Object {$_.State -eq "Running"}   }

但我不知道如何将所有结果放入哈希 table。如果你能帮助我,我将不胜感激。提前致谢:)

将其添加到末尾将输出哈希列表 tables:

 | foreach-object {
    $hash = @{}
    $object = $_
    $_ | Get-Member -MemberType noteproperty | Foreach-object { 
        $hash.($_.name) = $object.($_.name)
    }
    $hash
}

或者您可以创建一个散列 table 以进程 ID 作为键:

$bigHash = @{}
{your code} | foreach-object {
        $hash = @{}
        $object = $_
        $_ | Get-Member -MemberType noteproperty | Foreach-object { 
            $hash.($_.name) = $object.($_.name)
        }
        $bighash.([int]$hash.ProcessId) = $hash
    }