Powershell 命令输出选择字段并比较

Powershell 命令输出选择字段并比较
$csv1 = Import-Csv csv1.csv
$csv2 = Import-Csv csv2.csv
$merged = Merge-Csv -InputObject $csv1,$csv2 -ID "Invoice Number"
$merged | Select "Invoice Date","Invoice Number", @{name="CSV1InvStat";Expression={$_."CSV1 Invoice Status" -Replace "Open", "Unpaid" -Replace "Closed","Paid"}}, "CSV2 Invoice Status", "Customer", "Invoice Amount", "Paid Amount", @{Name=Diff;Expression={ $_."Invoice Amount" - $_."Paid Amount" }} | Sort "Invoice Number" | Export-Csv -encoding utf8 merged.csv

我得到的结果如下:

Invoice Date     Invoice Number     CSV1InvStat     CSV2 Invoice Status   CSV1Amount  ...
4/1/2016         INVPQ-0001         Paid            Unpaid                3,000.00
6/30/2016        HKXLY43901         Paid            Paid                  500

我想要做的是将“CSV1InvStat”字段与“CSV2 发票状态”进行比较进行替换:

$csv1 = Import-Csv csv1.csv
$csv2 = Import-Csv csv2.csv
$merged = Merge-Csv -InputObject $csv1,$csv2 -ID "Invoice Number"
$merged | Select "Invoice Date","Invoice Number", @{name="CSV1InvStat";Expression={$_."CSV1 Invoice Status" -Replace "Open", "Unpaid" -Replace "Closed","Paid"}}, "CSV2 Invoice Status",@{Name=StatusDiff;Expression={IF($_."CSV1InvStat" -eq $_."CSV2 Invoice Status") {0} Else {1}}}, "Customer", "Invoice Amount", "Paid Amount", @{Name=Diff;Expression={ $_."Invoice Amount" - $_."Paid Amount" }} | Sort "Invoice Number" | Export-Csv -encoding utf8 merged.csv

问题在于:

@{Name="StatusDiff";Expression={IF($_."CSV1InvStat" -eq $_."CSV2 Invoice Status") {0} Else {1}}}

我以为这样会在 merged.csv 中的状态后显示一列,如果状态不同则显示 1,如果状态相同则显示 0。但这似乎行不通,无论它们是否匹配,我都只得到 0。我认为这是因为 CSV1InvStat 在技术上并不存在于数据中。

有没有办法做到这一点,或者我最好让用户记住一个 excel 公式 (=IF(C2=D2,0,1)) 并手动执行此操作?

相关内容