简体   繁体   English

Powershell - Group-Object 展开并再次分组

[英]Powershell - Group-Object Expand and Group again

I have a.csv file which I'm grouping on two properties 'DN', 'SKU' and then performing a sum on another property 'DeliQty' This works fine and the sum is reflected back to the group.我有一个.csv 文件,我将其分组在两个属性“DN”、“SKU”上,然后对另一个属性“DeliQty”进行求和,这可以正常工作,并且总和会反映回组。 However I then need to re group just on 'DN' and write out to separate files.但是,然后我需要仅在“DN”上重新分组并写出单独的文件。 I've tried Select-Object -Expand Group but this reverts to the original contents without the summed lines.我已经尝试过 Select-Object -Expand Group 但这会恢复为原始内容而没有总和。

Is there a way to un group preserving the summed lines and then group again?有没有办法取消分组保留总和然后再次分组?

$CSVFiles = Get-ChildItem -Path C:\Scripts\INVENTORY\ASN\IMPORT\ -Filter *.csv

foreach ($csv in $CSVFiles) {
    $group = Import-Csv $csv.FullName | Group-Object DN, SKU
    $group | Where Count -gt 1 | ForEach-Object {
    $_.Group[0].'DeliQty' = ($_.Group | Measure-Object DeliQty -Sum).Sum 
    } 
    }

You may do the following:您可以执行以下操作:

$CSVFiles = Get-ChildItem -Path C:\Scripts\INVENTORY\ASN\IMPORT\ -Filter *.csv

foreach ($csv in $CSVFiles) {
    $group = Import-Csv $csv.FullName | Group-Object DN, SKU | Foreach-Object {
        if ($_.Count -gt 1) {
            $_.Group[0].DeliQty = ($_.Group | Measure-Object DeliQty -Sum).Sum
        }
        $_.Group[0]
    } 
    # outputs summed and single group objects
    $group
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM