PowerShell 比较对象不接受 2 个文件作为参数,然后进行比较



我正在编写一个PowerShell脚本,该脚本将以下内容转换为接受2个文件作为命令行参数的脚本,然后将两个文件的差异输出到文本文件。

目前,我毫无价值的代码如下:

# Compare two text files passed as command line arguments
Function compareFiles()
{
Param (
[Parameter(mandatory=$true)]
[string]$file1,
[string]$file2 
)
$file1 = resolve-path .$file1
$file2 = resolve-path .$file2
if ($file1 -eq $null) {
Write-Host "No source file."
}
if ($file2 -eq $null) {
Write-Host "No comparison file."
}
# Compare the two file objects
# => means $file2 has that line but not $file1
# =< means $file1 has that line but not $file2
# to see lines that arw the same, add -includeequal to end of the following line
Compare-Object -ReferenceObject (Get-Content $file1) -DifferenceObject (Get-Content $file2) | Out-File "$file1_$file2_Compare.txt" -Force
}

在 PowerShell 控制台中,定义$obj1="test1.txt"$obj2="test2.txt"后,行Compare-Object -ReferenceObject (Get-Content $obj1) -DifferenceObject (Get-Content $obj2工作正常。

当我从 PowerShell 控制台运行.Compare-Files.ps1 -file1 test1.txt -file2 test2.txt时,我得到以下结果:

compareFiles : Cannot bind argument to parameter 'file1' because it is an empty string.
At C:Usersdebug255DocumentsFiverrPowerShellOrdersarmytra1n3dFO4ADB39A404Compare-Files.ps1:27 char:21
+ compareFiles -file1 $file1 -file2 $file2 | Out-File "$file1_$file2_Co ...
+                     ~~~~~~
+ CategoryInfo          : InvalidData: (:) [compareFiles], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,compareFiles

此错误已通过从早期测试中删除剩余的$obj1$obj2变量得到解决

这个脚本来自 Hey Scripting Guy 工作正常

$fileA = "test1.txt"
$fileB = "test2.txt"
if(Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB))
{
"files are different"
}
Else
{
"Files are the same"
}

test1.txt包含以下内容:

test1
1
2
3
4
5
6
7
asaskjdahs
sl kasldk

test2.txt 包含:

test2
1
23
3
4
5
6
799
asaskjdahs
sl kasldkJ

我在Windows 10上使用PowerShell 5.1.14393.1066。

感谢您的帮助!

编辑

我完全更改了脚本,因此没有用于比较的功能,并使用了我在GitHub上找到的示例来打开和比较MS Word文档,这有很大帮助。

# Compare two text files passed as command line arguments
Param (
[Parameter(mandatory=$true)]
$file1,
$file2
)
$ErrorActionPreference = 'Stop'
# Remove read-only attributes from baseFile
$baseFile = Get-ChildItem $file1
if ($baseFile.IsReadOnly) {
$baseFile.IsReadOnly = $false
}
try {
Compare-Object -ReferenceObject (Get-Content $file1) -DifferenceObject (Get-Content $file2) `
| Out-File $file1"_"$file2"_Comparison.txt" -Force
}
catch {
Write-Host "There was an error"
}

但是,一旦我将代码包装在函数中,就像compareFiles() { ... }一样,当我运行相同的行时,它确实有效,而没有将其包含在函数中,.Compare-Files.ps1 -file1 test1.txt -file2 test2.txt,没有任何输出。当我在函数compareFiles下方的脚本中添加函数调用时,它会提示我$file1,失败并且没有像以前那样提示我$file2,只是这次我的catch抓住了它。

然后,我通过将Param ()放在函数compareFiles上方来使全局,并且它起作用了!

现在,我需要专注于清理输出。输出如下:

InputObject    SideIndicator
-----------    -------------
test2      =>           
1          =>           
23         =>           
3          =>           
4          =>           
5          =>           
6          =>           
799        =>           
asaskjdahs =>           
sl kasldkJ =>           
test1          <=           
1              <=           
2              <=           
3              <=           
4              <=           
5              <=           
6              <=           
7              <=           
asaskjdahs     <=           
sl kasldk      <=           

如果你们中的任何人有任何建议使输出看起来像这样:

test2.txt:
InputObject    SideIndicator
-----------    -------------
test2      =>           
1          =>           
23         =>           
3          =>           
4          =>           
5          =>           
6          =>           
799        =>           
asaskjdahs =>           
sl kasldkJ =>
test1.txt
test1      <=           
1          <=           
2          <=           
3          <=           
4          <=           
5          <=           
6          <=           
7          <=           
asaskjdahs <=           
sl kasldk  <=  

另外,我注意到它说test1.txt在第 1、2、3、4 行等上有不同的内容,但它们实际上是相同的(例如,test1.txt中的1test2.txt相同且在同一行)。

有什么建议可以清理一下吗?

谢谢大家的帮助!

注意:问题不断变化,代码表现出各种不相关的问题,因此很难给出有意义的答案,但这里有一些提示:

  • Param ( [Parameter(mandatory=$true)] $file1, $file2 )仅将$file1定义为必需的,而不是$file2- 每个参数都需要自己的[Parameter(...)]属性。

  • .$file1.$file2不能按预期的方式工作;也许你的意思是.$file1.$file2引用当前目录中的文件,但你不需要任何前缀,因为无论如何Resolve-Path默认为当前目录。

  • 测试$file1 -eq $null$file2 -eq $null永远不会成功,因为$file1$file2由于已声明为[string]参数,永远不会$null

    • PowerShell 中的[string]类型变量默认为空字符串而不是$null,甚至显式分配$null也会导致空字符串(将其与 C# 进行比较,其中string变量默认为null,并且可以分配null)。
  • 在字符串"$file1_$file2_Compare.txt"中展开(内插)$file1$file2变量将不起作用,因为,鉴于_是变量名称中的合法字符,则必须告诉 PowerShell 变量名称的结束位置,方法是将它们括在{...}中:"${file1}_${file2}_Compare.txt"

最新更新