Powershell transpose rows into columns

后端 未结 1 1279
耶瑟儿~
耶瑟儿~ 2021-01-29 11:08

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

<#MachineName, TotalDataSizeBytes, A         


        
相关标签:
1条回答
  • 2021-01-29 11:30

    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
    

    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
    }
    

    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
    
    0 讨论(0)
提交回复
热议问题