使用PowerShell将数据表导出到csv不完整

wgx48brx  于 2023-09-27  发布在  Shell
关注(0)|答案(1)|浏览(126)

PowerShell 5.1
我将datatable $dt_1和$dt_2加入到一个新的datatable $中,并与以下代码合并:

$merged = $dt_1 | ForEach-Object {
$dt1Row = $_
$dt2Rows = $dt_2 | Where-Object { $_."Facility" -eq $dt1Row."Facility" -and $_."Customer" -eq $dt1Row."Customer" -and $_."Contract" -eq $dt1Row."Contract" }

$resultObject = $dt1Row | Select-Object  "Contract", "Customer ","Contract"
$Count = 1

foreach ($dt2Row in $dt2Rows) {
    $resultObject | Add-Member -MemberType NoteProperty -Name ("Material - $Count") -Value $dt2Row.Material
    $resultObject | Add-Member -MemberType NoteProperty -Name ("Material_Number - $Count") -Value $dt2Row.'Material_Number'
    $Count ++

}

$resultObject
}

$dt_2具有存储在$dt1中的密钥的多个条目:设施、客户、合同。
当我输出$merged与

$merged | Format-Table -AutoSize

在控制台中,我得到了我想要导出到CSV的视图:
| 设施|客户|合同|资料-1|材料_编号-1|资料-2|材料_编号-2|资料-3|材料_编号-3|
| --|--|--|--|--|--|--|--|--|
| 面_A|客户_X|合同_X|材料_A|材料编号123|材料_B|材料编号234|材料_C|材料编号564|
| 面_A|客户_Y|合同_Y|材料_A|材料编号123|材料_B|材料编号234|||
但是如果我通过以下方式将$merged导出到csv

$merged | Export-Csv "\Path\To\CSV.csv" -NoType -Delimiter ";" -Encoding UTF8

我在CSV中只得到最大为1的值:
| 设施|客户|合同|资料-1|材料_编号-1|
| --|--|--|--|--|
| 面_A|客户_X|合同_X|材料_A|材料编号123|
| 面_A|客户_Y|合同_Y|材料_A|材料编号123|
这是什么原因,我可以强制完全导出到csv吗?

prdp8dxp

prdp8dxp1#

正如我在评论中提到的,我猜你缺少的属性是因为你集合中的第一个项目没有其他项目那么多的属性。我对您的流程有一个稍微不同的解决方案,但它仍然迭代$dt_2,并根据$dt_1进行验证(看起来您正在这样做)。它将事物分组,找到最大的组,然后为每个组创建对象,其中包含最大组所具有的材料数量(只是在额外的列中具有空值)。首先,我模拟了与您的示例中类似的表。我在第一个表中添加了一对夫妇,以包含一个相同的客户/不同的设施,以显示如何输出,以及一个在表$dt_2中没有匹配的客户。我还在$dt_2中添加了一行,但在$dt_1中没有匹配项,以显示它将从输出中排除,就像您的代码一样。

$dt_1=@'
"Facility","Customer","Contract"
"Seattle","Cust_A","C1"
"Chicago","Cust_A","C1"
"Chicago","Cust_B","C2"
"Dallas","Cust_C","C3"
"Dallas","Cust_D","C2"
'@|convertfrom-csv

$dt_2=@'
"Facility","Customer","Contract","Material","Material_Number"
"Seattle","Cust_A","C1","M1","123"
"Chicago","Cust_B","C2","M1","123"
"Seattle","Cust_A","C1","M2","456"
"Dallas","Cust_C","C3","M2","456"
"Dallas","Cust_C","C3","M4","789"
"Chicago","Cust_A","C1","M2","456"
"Chicago","Cust_A","C1","M5","000"
"Seattle","Cust_D","C2","M4","789"
'@|ConvertFrom-Csv

现在我将$dt_2分组,找到最大的组,并为所有组创建对象。

$Grouped = $dt_2|where{$dt_1 -match ($_|select "Facility","Customer","Contract")}|group Facility,Custom,Contract
$MaxGroup = $Grouped|measure-object -Maximum -Property Count|% Maximum
$Results = $Grouped|%{
    $objOut = $_.Group[0] |Select "Facility", "Customer","Contract"
    For($i=0;$i -lt $MaxGroup;$i++){
        Add-Member -InputObject $objOut -NotePropertyName "Material - $($i+1)" -NotePropertyValue $_.group[$i].Material
        Add-Member -InputObject $objOut -NotePropertyName "Material_Number - $($i+1)" -NotePropertyValue $_.group[$i].Material_Number
    }
    $objOut
}

以下是通过管道传输到Format-TableConvertTo-Csv的结果(您可以使用Export-Csv将结果写入文件而不是屏幕)

PS C:\Users\TMTech> $Results | Format-Table -AutoSize

Facility Customer Contract Material - 1 Material_Number - 1 Material - 2 Material_Number - 2
-------- -------- -------- ------------ ------------------- ------------ -------------------
Seattle  Cust_A   C1       M1           123                 M2           456                
Chicago  Cust_B   C2       M1           123                                                 
Dallas   Cust_C   C3       M2           456                 M4           789                
Chicago  Cust_A   C1       M2           456                 M5           000                

PS C:\Users\TMTech> $Results | convertto-csv -NoTypeInformation -Delimiter ';'

"Facility";"Customer";"Contract";"Material - 1";"Material_Number - 1";"Material - 2";"Material_Number - 2"
"Seattle";"Cust_A";"C1";"M1";"123";"M2";"456"
"Chicago";"Cust_B";"C2";"M1";"123";;
"Dallas";"Cust_C";"C3";"M2";"456";"M4";"789"
"Chicago";"Cust_A";"C1";"M2";"456";"M5";"000"

请注意,第一个和最后一个结果都是Cust_A,只是针对不同的设施点,显示了在哪个设施点使用了哪些材质。

相关问题