对象比较不适用于多个列

时间:2017-08-16 16:25:10

标签: powershell csv comparison

我的脚本必须比较2个CSV文件中具有不同标题名称的2列,并将差异写入txt文件。这是我的代码:

Write-Host "Script output will have unique items from file 1"
$FirstPath = Read-Host -Prompt "Input file location of first .csv file"
$SecondPath = Read-Host -Prompt "Input file location of second .csv file"

Write-Host "Importing CSV files..."
$FirstFile = Import-Csv $FirstPath
$SecondFile = Import-Csv $SecondPath 

Write-Host "Comparing files..."
$SecondFile | Add-Member -MemberType AliasProperty -Name Url -Value Url2
Compare-Object -ReferenceObject $FirstFile -DifferenceObject $SecondFile -Property Url -PassThru |
    Where-Object{$_.SideIndicator -eq "<="} |
    Out-File .\result.txt

Write-Host "Done, press any key to continue..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

到目前为止我想到了什么:

如果每个文件中只有1列,则一切正常。 2个不同标题名称的别名起作用,搜索工作,也写入输出文件。

问题是当我尝试搜索超过1列的CSV文件时。如果包含别名的CSV文件超过2列,则会出现此错误:

  

会员&#39; Url2&#39;不存在

如果第一个文件 - ReferenceObject有两列以上,则输出是整个第一个文件。

修改

我比较的2个示例CSV文件:

Url;Fail 
test1;failvalue1 
test2;failvalue2
Url2;Fail2
test1;failvalue1
test3;failvalue2

输出应仅为&#34; test1&#34;,因为我正在比较&#34; Url&#34;用&#34; Url2&#34;通过别名属性。如果我删除&#34;失败&#34;和&#34;失败2&#34;列,每个文件只留下1列,一切都如前所述。

0 个答案:

没有答案