Powershell表 - 行到列

时间:2014-09-25 14:15:59

标签: powershell

我有像对象一样输出的对象数组

$VM | %{ $_ | Get-Stat -Stat $metrics -Realtime -Start (Get-Date).AddMinutes(-2) -Instance ""} | Format-Table -AutoSize

MetricId                Timestamp                          Value Unit     
--------                ---------                          ----- ----     
cpu.ready.summation     25.9.2014 15:23:40                    39 milli      
cpu.ready.summation     25.9.2014 15:23:20                    50 milli    
cpu.ready.summation     25.9.2014 15:23:00                    36 milli   
cpu.ready.summation     25.9.2014 15:22:40                    44 milli   
cpu.ready.summation     25.9.2014 15:22:20                    43 milli    
cpu.ready.summation     25.9.2014 15:22:00                    46 milli   
cpu.demand.average      25.9.2014 15:23:40                    37 MHz              
cpu.demand.average      25.9.2014 15:23:20                    37 MHz              
cpu.demand.average      25.9.2014 15:23:00                    37 MHz              
cpu.demand.average      25.9.2014 15:22:40                    38 MHz              
cpu.demand.average      25.9.2014 15:22:20                    40 MHz              
cpu.demand.average      25.9.2014 15:22:00                    41 MHz              
cpu.usagemhz.average    25.9.2014 15:23:40                    26 MHz              
cpu.usagemhz.average    25.9.2014 15:23:20                    52 MHz              
cpu.usagemhz.average    25.9.2014 15:23:00                    29 MHz              
cpu.usagemhz.average    25.9.2014 15:22:40                    28 MHz              
cpu.usagemhz.average    25.9.2014 15:22:20                    51 MHz              
cpu.usagemhz.average    25.9.2014 15:22:00                    32 MHz          

如何像这样更改输出?

25.9.2014 15:23:40  39 milli    37 MHz      26 MHz
25.9.2014 15:23:20  50 milli    37 MHz      52 MHz
.
.
.

2 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是创建新集合并根据需要手动设置对象格式:

$raw = $VM | %{ $_ | Get-Stat -Stat $metrics -Realtime -Start (Get-Date).AddMinutes(-2) -Instance ""}

$joined = @()

foreach ($raw_object in $raw) {
    $need_new_object = ($joined | Select-Object -ExpandProperty Timestamp) `
        -notcontains $raw_object.Timestamp
    $value = $raw_object.Value + " " + $raw_object.Unit

    if ($need_new_object) {
        $object = New-Object PSObject -Property @{
            "Timestamp" = $raw_object.Timestamp
        }

        $joined += $object
    } else {
       $object = `
            $joined | Where-Object { $_.Timestamp -eq $raw_object.Timestamp }
    }

    $object | Add-Member -MemberType "NoteProperty" `
        -ErrorAction "SilentlyContinue" -Name $raw_object.MetricId -Value $value

}

$joined | Format-Table -AutoSize

输出正是您想要的:

Timestamp          cpu.ready.summation cpu.demand.average cpu.usagemhz.average
---------          ------------------- ------------------ --------------------
25.9.2014 15:23:40 39 milli            37 MHz             26 MHz              
25.9.2014 15:23:20 50 milli            37 MHz             52 MHz              
25.9.2014 15:23:00 36 milli            37 MHz             29 MHz              
25.9.2014 15:22:40 44 milli            38 MHz             28 MHz              
25.9.2014 15:22:20 43 milli            40 MHz             51 MHz              
25.9.2014 15:22:00 46 milli            41 MHz             32 MHz    

答案 1 :(得分:1)

看起来是在TimeStamp上使用Group-Object的好时机,然后在每个时间戳创建一个对象,并为该时间戳的每条记录添加成员:

$data = $VM | %{ $_ | Get-Stat -Stat $metrics -Realtime -Start (Get-Date).AddMinutes(-2) -Instance ""}
$Converted = $Data | Group TimeStamp | %{
    $Record = [pscustomobject][ordered]@{'TimeStamp'=$_.Name}
    $_.Group |%{
        Add-Member -InputObject $Record -MemberType NoteProperty -Name $_.MetricId -Value $_.Value
        Add-Member -InputObject $Record -MemberType NoteProperty -Name ($_.MetricId + ' Unit') -Value $_.Unit
    }
    $Record
}
$Converted|ft

那会输出:

TimeStamp          cpu.ready.summation cpu.ready.summation Unit cpu.demand.average cpu.demand.average Unit cpu.usagemhz.average cpu.usagemhz.average Unit
---------          ------------------- ------------------------ ------------------ ----------------------- -------------------- -------------------------
25.9.2014 15:23:40 39                  milli                    37                 MHz                     26                   MHz                      
25.9.2014 15:23:20 50                  milli                    37                 MHz                     52                   MHz                      
25.9.2014 15:23:00 36                  milli                    37                 MHz                     29                   MHz                      
25.9.2014 15:22:40 44                  milli                    38                 MHz                     28                   MHz                      
25.9.2014 15:22:20 43                  milli                    40                 MHz                     51                   MHz                      
25.9.2014 15:22:00 46                  milli                    41                 MHz                     32                   MHz                      

列标题有点长,但您可以使用某些子字符串或者替换shenanigans来解析它们。

修改:为了缩短标题,您可以将$_.MetricId的{​​{1}}实例替换为$_.MetricId.Split('.')[1],并将它们缩短为“就绪”,“需求”,和'usagemhz'及其相关的单元标题同样命名。这使它更具可读性。