6

这是我写的脚本:

function Compare {

    $file1 = Read-Host "Please enter the path of the first file you would like to compare"
    $file2 = Read-Host "Please enter the path of the second file you would like to compare"

    $outFile = Read-Host "Please enter the path to where you would like your output file."

    Try{
        $compareOne = Get-Content $file1
        $comparetwo = Get-Content $file2
    } 
    Catch{
        Write-Host "The path you entered is either invalid or the file does not exist. "    
    }

    Write-Host "Beginning comparison"
    Compare-Object $compareOne $compareTwo  | Out-File $outFile
    Write-Host "Complete!"
}

Compare

这是我的输出:

InputObject | SideIndicator
------------|--------------
   Value1   |    <=
   Value2   |    <=
   Value3   |    =>
   Value4   |    =>

我是否可以以可以更改每列标题的方式格式化我的输出?

而不是=><=我可以给出差异实际上是在哪个文件中找到的?

这是我正在寻找的输出类型:

   Value    |     File 
------------|--------------
   Value1   |    $file1
   Value2   |    $file2
   Value3   |    $file2
   Value4   |    $file1

我对 PowerShell 还是很陌生,所以如果你能解释你的答案那就太好了,这样我就可以理解实际发生了什么。

我也在尝试制作这个“虚拟证明”,以便任何人都可以比较两个文本文件,而无需进一步输入。

任何帮助将不胜感激!

4

2 回答 2

7

像这样的东西,也许?

function compareCSV {

$file1 = Read-Host "Please enter the path of the first file you would like to compare"
$file2 = Read-Host "Please enter the path of the second file you would like to compare"

$outFile1 = Read-Host "Please enter the path to where you would like your output file."

Try{
    $compareOne = Get-Content $file1
    $comparetwo = Get-Content $file2
} 
Catch{
    Write-Host "The path you entered is either invalid or the file does not exist. "    
}

Write-Host "Beginning comparison"
$Compare = 
Compare-Object $compareOne $compareTwo

$compare | foreach  { 
      if ($_.sideindicator -eq '<=')
        {$_.sideindicator = $file1}

      if ($_.sideindicator -eq '=>')
        {$_.sideindicator = $file2}
     }

 $Compare | 
   select @{l='Value';e={$_.InputObject}},@{l='File';e={$_.SideIndicator}} |
   Out-File $outFile1

  Write-Host "Complete!"
}

compareCSV
于 2014-03-06T16:39:45.123 回答
4

改变这个:

Compare-Object $compareOne $compareTwo  | Out-File $outFile

有了这个:

Compare-Object $compareOne $compareTwo  | 
ft inputobject, @{n="file";e={ if ($_.SideIndicator -eq '=>') { "$file2" }  else { "$file1" } }} | Out-File $outFile
于 2014-03-06T16:27:06.873 回答