我有一个比较2个XML文件的脚本。现在,我已经对文件位置进行了硬编码,但是我需要找到一种以某种方式调用它们的方法。也许使用参数会起作用?我尝试玩耍,但无法进行任何工作。我知道使用斜杠以某种方式引用它是有问题的吗?我对PS真的很陌生,不确定如何去做。
这就是我所拥有的:
[xml]$file1 = Get-Content "C:\Generic.xml"
[xml]$file2 = (Get-Content "C:\Comparison.xml")
$file1
$file2
$file1.configuration
$Compare = Compare-Object -ReferenceObject $file1.configuration.ChildNodes -DifferenceObject $file2.configuration.ChildNodes -IncludeEqual
if ($compare.SideIndicator -eq "==") {
"There is a match for all of the child nodes"
}
Else {
"There is a child node missing"
}
$file1.configuration.appSettings.add | more
$file2.configuration.appSettings.add | more
$Compare = Compare-Object -ReferenceObject $file1.configuration.appSettings.add -DifferenceObject $file2.configuration.appSettings.add -Property key,value -IncludeEqual
foreach ($compare in $Compare) {
if ($compare.SideIndicator -eq "==") {
"There is a match for key $($compare.key) and value $($compare.value)"
}
if ($compare.SideIndicator -eq "<=") {
"There is no match for key $($compare.key) and value $($compare.value)"
}
}
任何建议将不胜感激!
答案 0 :(得分:0)
您可以将其包装在一个函数中,这将允许您使用所选的可变参数执行脚本:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-6
因此,在您的函数中,您将使用大多数相同的代码,但可能需要修改:
[xml]$file1 = Get-Content "C:\Generic.xml"
到
[xml]$file1 = Get-Content $file1_path
然后使用以下格式执行:
Compare-XML -file1_path "C:\Generic.xml"