在发布之前,我做了很多搜索,没有运气。
假设我要比较两个文件夹
compare-object @(gci c:\folder1) @(gci c:\folder2)
我看到sideIndicator等的输出,但是我该怎么做
if objects are equals then
do something
else
do something else
end if
如果我将此cmdlet传递给get-member,我会看到一个equals方法,但我不知道如何使用它,甚至不确定它是否可以解决我的问题。是否有任何 bool 属性可以告诉我对象是否相等?
谢谢你的时间。
编辑
嗨,我的救星。 :)
如果我了解我必须做这样的事情:
$differences = 0
$a = @(1,4,5,3)
$b = @(1,3,4)
diff $a $b -in | % {if($_.sideindicator -ne "==") {$differences+=1}}
write-host $differences
这是唯一的方法吗?
编辑2.
在我看来,如果我不使用-includeEqual,我只会得到区别,所以这段代码就足够了。
$differences = 0
$a = @(1,4,5,3)
$b = @(1,3,4)
diff $a $b | % {$differences+=1}
write-host $differences
如果对象相等,则返回0。这是解决我的问题的正确方法吗?
最佳答案
基本上,您不需要计算差异,相反,如果仅找到'=='就足够了,则可以使用:
$a = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
$b = @(10, 1, 2, 3, 4, 5, 6, 7, 8, 9)
if (diff $a $b) {
'not equal'
} else {
'equal'
}
工作原理:如果管道(来自diff的序列)返回0项的集合,那么当转换为
[bool]
时,它将转换为$false
。非空集合将转换为$true
。
关于powershell - 如何处理比较对象结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5805922/