Powershell将行转置为列 [英] Powershell transpose rows into columns

查看:564
本文介绍了Powershell将行转置为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我把行转换成colums。需要将MachineName转置为列。

Endtime必须排序。

Can someone help me to transpose row into colums. Need to transpose MachineName into columns.
Endtime must be sorted.

<#MachineName, TotalDataSizeBytes, ActualStartTime, EndTime,   FinalJobStatus
SERVER1, 322349304901, 28/02/2016 23:00:03, 29/03/2016 23:33:23, OK
SERVER1, 322349304902, 26/02/2016 23:00:03, 27/03/2016 23:33:23, OK
SERVER2, 322349304903, 28/02/2016 23:00:01, 29/03/2016 23:33:23, OK
SERVER2, 322349304904, 26/02/2016 23:00:01, 27/03/2016 23:33:23, OK
#>

$graph = Import-Csv d:\report3.csv | foreach {

  New-Object PSObject -prop @{
    EndTime = [DateTime]::Parse($_.EndTime);
    MachineName = $_.MachineName;
    TotalDataSizeBytes = $_.TotalDataSizeBytes
  }
} 

#Desired result in a csv file.

#EndTime,SERVER1,SERVER2
#27/03/2016 23:33:23,322349304902,322349304904
#29/03/2016 23:33:23,322349304901,322349304903


Edit1. Found a similar question:
http://stackoverflow.com/questions/33906500/powershell-csv-row-column-transpose-and-manipulation


推荐答案

从找到所有相关的机器名开始,我们以后需要这些:

Start by finding all relevant machine names, we'll need those later:

$Rows = Import-Csv d:\report3.csv
$MachineNames = $Rows |Select-Object -ExpandProperty MachineName |Sort -Unique

接下来,通过Endtime对所有条目进行分组,并将组合在一起的所有行的值添加到单个对象中: / p>

Next up, group all entries by Endtime, and add the values from all rows that are grouped together into a single object:

$ConsolidatedRows = $Rows |Group-Object EndTime |ForEach-Object {
    $NewRowProperties = @{ EndTime = [DateTime]::Parse($_.Name) }
    foreach($Row in $_.Group)
    {
        $NewRowProperties.Add($Row.MachineName,$Row.TotalDataSizeBytes)
    }
    New-Object psobject -Property $NewRowProperties
}

然后最后,使用我们之前抓取的名称选择 EndTime 属性和所有 MachineName 特定属性: / p>

And then finally, select the EndTime property and all the MachineName-specific properties using the names we grabbed earlier:

$ConsolidatedRows |Select-Object @("EndTime";$MachineNames) |Export-Csv .\finalReport.csv -NoTypeInformation

这篇关于Powershell将行转置为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆