如何遍历哈希表中的哈希表

时间:2019-09-12 08:42:46

标签: powershell-2.0

我有一个哈希表,该哈希表的键为整数,值为哈希表。 嵌套哈希表有3个列名,cpu%。

我需要创建一个仅包含所有嵌套哈希表中的第一个值的新哈希表

Name Value
---- -----
4    {@{Name=Windows PowerShell ISE; CPU=1.54 %}, @{Name=Windows PowerShell ISE; CPU=1.54 %},@{Name=Windows PowerShell ISE; CPU=1.54 %}
3    {@{Name=Windows PowerShell ISE; CPU=1.54 %}, @{Name=Windows PowerShell ISE; CPU=1.54 %},@{Name=Windows PowerShell ISE; CPU=1.54 %}
2    {@{Name=Windows PowerShell ISE; CPU=1.54 %}, @{Name=Windows PowerShell ISE; CPU=1.54 %},@{Name=Windows PowerShell ISE; CPU=1.54 %}
1    {@{Name=Windows PowerShell ISE; CPU=1.54 %}, @{Name=Windows PowerShell ISE; CPU=1.54 %},@{Name=Windows PowerShell ISE; CPU=1.54 %}
0    {@{Name=Windows PowerShell ISE; CPU=1.54 %}, @{Name=Windows PowerShell ISE; CPU=1.54 %},@{Name=Windows PowerShell ISE; CPU=1.54 %}

我已经遍历了第一个哈希表,但无法弄清楚如何仅从嵌套表中获取第一个值。

$sleep_time = 5
$repeat_number = 5
$hash = @{}

for ($i=0; $i -le $repeat_number-1; $i++) {
    $tempRes = Get-Counter "\Process(*)\% Processor Time" -ErrorAction SilentlyContinue |
               select -ExpandProperty CounterSamples |
               where {$_.Status -eq 0 -and $_.instancename -notin "_total", "idle"} |
               sort CookedValue -Descending |
               select TimeStamp, @{N="Name";E={
                   $friendlyName = $_.InstanceName
                   try {
                       $procId = [System.Diagnostics.Process]::GetProcessesByName($_.InstanceName)[0].Id
                       $proc = Get-WmiObject -Query "SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId=$procId"
                       $procPath = ($proc | where { $_.ExecutablePath } | select -First 1).ExecutablePath
                       $friendlyName = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($procPath).FileDescription
                   } catch { }
                   $friendlyName
               }}, @{N="CPU";E={($_.CookedValue/100/$env:NUMBER_OF_PROCESSORS).ToString("P")}} -First 5

    $hash.$i = $tempRes
    Start-Sleep -s 1
}

$hash

$hash是主要的哈希表。这包含4个条目。 每个条目都有键值,值有5个哈希表。

我需要从嵌套哈希表中选择第一个哈希表。

0 个答案:

没有答案
相关问题