BRB*_*RBT 4 powershell output compareobject
这是我写的脚本:
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
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
InputObject | SideIndicator
------------|--------------
Value1 | <=
Value2 | <=
Value3 | =>
Value4 | =>
Run Code Online (Sandbox Code Playgroud)
我可以以这样的方式格式化输出,以便我可以更改每列的标题吗?
而不是=>和<=我可以给哪个文件的差异中居然发现?
这是我正在寻找的那种输出:
Value | File
------------|--------------
Value1 | $file1
Value2 | $file2
Value3 | $file2
Value4 | $file1
Run Code Online (Sandbox Code Playgroud)
我仍然是PowerShell的新手,所以如果你能解释一下你的答案会很棒,那么我就能明白实际发生了什么.
我也试图制作这个"虚拟证明",这样任何人都可以比较两个文本文件,而无需任何进一步的输入.
任何帮助将不胜感激!
也许是这样的事情?
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20185 次 |
| 最近记录: |