gpt4 book ai didi

PowerShell Compare-Object 和 Export-Csv 导出错误数据

转载 作者:行者123 更新时间:2023-12-04 08:17:28 25 4
gpt4 key购买 nike

问题
我有两个 CSV 文件,文件 A 和文件 B。两个文件都包含相同的标题。
这些文件包含有关报价和订单的信息。
首先创建文件 A,假设是上午 10:00。文件 B 是在上午 11:00 创建的。那是状态列值从“报价”更新为“订单”的时候,也许还有其他一些细节。
我用 Compare-ObjectExport-Csv合并以将差异导出到新的 CSV 文件,但只应导出最新(最新)信息。
问题是:Compare-Object正确检测到特定行已更改,但不是使用文件 B 中的数据,而是使用文件 A(旧版本)中的数据。
示例(文件内容)
我用粗体突出显示了更改的字段。
文件 A

"CustomerName","Address","Postalcode","City","ReferenceNumber","CustomerNumber","Statuscode","DeliveryWeek","WorkDescription","Status","OrderReference","Advisor"  "Example Customer","Example Address 1","9999 EX","EXAMPLE CITY","217098","8629",**"Quote"**,**""**,"Example Product","Example Status","Private","Example Advisor"

File B

"CustomerName","Address","Postalcode","City","ReferenceNumber","CustomerNumber","Statuscode","DeliveryWeek","WorkDescription","Status","OrderReference","Advisor"  "Example Customer","Example Address 1","9999 EX","EXAMPLE CITY","217098","8629",**"Order"**,**"Call-off"**,"Example Product","Example Status","Private","Example Advisor"

Script

OK there are quite some lines there. I’ll share the lines where I believe the issue should reside.

$timestamp = (get-date -UFormat "%A %d-%m-%Y %R" | ForEach-Object { $_ -replace ":", "-" })
$prefix="Export-"
$exportlocation = "C:\Users\username\Desktop\UTF8-format\"
$ExportChangesFolder = "C:\Users\username\Desktop\Changes\"

$PreviousCSV = Import-Csv $PreviousFile -Header "CustomerName","Address","Postalcode","City","ReferenceNumber","CustomerNumber","Statuscode","DeliveryWeek","WorkDescription","Status","OrderReference","Advisor"
$NewCSV = Import-Csv $exportlocation$prefix$timestamp".csv" -Header "CustomerName","Address","Postalcode","City","ReferenceNumber","CustomerNumber","Statuscode","DeliveryWeek","WorkDescription","Status","OrderReference","Advisor"

$propsToCompare = $PreviousCSV[0].psobject.properties.name
Compare-Object -ReferenceObject $PreviousCSV -DifferenceObject $NewCSV -Property $propsToCompare -PassThru | select $propsToCompare | sort -Unique -Property "ReferenceNumber" | Select-Object * -ExcludeProperty SideIndicator | Export-Csv $ExportChangesFolder$prefix$timestamp".csv" -NoTypeInformation
通常,所有文件名都会自动填充,因为这是使用 Windows 任务计划程序的重复任务设置。在故障排除期间,我手动填写了声明变量的文件名。每次我手动运行它时,它都能正常工作!

最佳答案

我认为您可能缺少的是 SideIndicator .您应该可以只选择 SideIndicators 的列表你想要“<= ”是只存在于左边csv的东西,“=> ”是只存在于右边的东西。
看起来您也在指定标题,然后从 csv 中获取标题,但是您提到它们具有相同的标题?Get-Date在运行时针对 Import-Csv 的现有文件也有点令人困惑,但我猜想在导入之前构建这个 csv 的脚本还有更多内容,Get-Date运行。
这是对我有用的东西:

$timestamp = ((get-date -UFormat "%A %d-%m-%Y %R") -replace ":", "-")
$prefix="Export-"
$exportLocation = "C:\Users\username\Desktop\UTF8-format\"
$exportChangesFolder = "C:\Users\username\Desktop\Changes\"

$headers = $previousCSV[0].psobject.properties.name

$previousCSV = Import-Csv $previousFile
$newCSV = Import-Csv $exportLocation$prefix$timestamp".csv"

$compareParams = @{
ReferenceObject = $previousCSV
DifferenceObject = $newCSV
Property = $headers
PassThru = $true
}

Compare-Object @compareParams |
Where-Object {$_.SideIndicator -eq "=>"} |
Select-Object $headers |
Sort-Object -Unique -Property "ReferenceNumber" |
Select-Object * -ExcludeProperty SideIndicator |
Export-Csv $exportChangesFolder$prefix$timestamp".csv" -NoTypeInformation

关于PowerShell Compare-Object 和 Export-Csv 导出错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65648453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com