且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell将行转置为列

更新时间:2023-11-18 21:30:04

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

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