简体   繁体   English

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

[英]PowerShell Compare-Object and Export-Csv exporting wrong data

Problem问题

I have two CSV files, file A and file B. Both files contain the same header.我有两个 CSV 文件,文件 A 和文件 B。两个文件都包含相同的 header。
The files contain information about quotes and orders.这些文件包含有关报价和订单的信息。

File A was created first, at let's say 10:00 AM.首先在上午 10:00 创建文件 A。 File B was created at 11:00 AM.文件 B 于上午 11:00 创建。 That's when the status column value updates from 'Quote' to 'Order', and maybe some other details as well.那是状态列值从“报价”更新为“订单”的时候,也许还有其他一些细节。

I use Compare-Object and Export-Csv combined to export the differences to a new CSV file, but only the newest (up to date) information should be exported.我结合使用Compare-ObjectExport-Csv将差异导出到新的 CSV 文件,但仅应导出最新(最新)信息。
The problem is: Compare-Object correctly detects that a specific row has been changed, but instead of using the data from file B, it is using the data from file A (the older version).问题是: Compare-Object正确检测到特定行已更改,但不是使用文件 B 中的数据,而是使用文件 A(旧版本)中的数据。

Example (file contents)示例(文件内容)

I have highlighted the fields that have changed in bold.我以粗体突出显示了已更改的字段。

File 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文件 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 

Normally, all file names are populated automatically, as this is a recurring task setup using Windows Task Scheduler.通常,所有文件名都会自动填充,因为这是使用 Windows 任务计划程序的重复任务设置。 During troubleshooting I have manually filled in the file names where the variables are declared.在故障排除期间,我手动填写了声明变量的文件名。 And everytime I run it manually, it works fine!每次我手动运行它时,它都可以正常工作!

I think what you may be missing is the SideIndicator .我认为您可能缺少的是SideIndicator You should be able to just choose the list of SideIndicators you want with " <= " being the things that exist only in the left csv and " => " being the things that only exist in the right.您应该能够只选择您想要的SideIndicators列表,其中“ <= ”是仅存在于左侧 csv 中的事物,而“ => ”是仅存在于右侧的事物。

It looks like you are also specifying headers and then grabbing the headers from the csv, but you mentioned they have the same headers?看起来您还指定了标头,然后从 csv 中获取标头,但您提到它们具有相同的标头?

The Get-Date at runtime targeting an existing file for Import-Csv is also a bit confusing, but I'm guessing there's more to the script that builds this csv before it's imported and Get-Date runs.运行时针对Import-Csv的现有文件的Get-Date也有点令人困惑,但我猜在导入和Get-Date运行之前构建此 csv 的脚本还有更多内容。

Here's something that is working on my end:这是我正在做的事情:

$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

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

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