比较两个PsCustomObjects的属性 [英] Compare the properties of two PsCustomObjects
本文介绍了比较两个PsCustomObjects的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以比较两个PowerShell对象的值:
I know that I can compare the values of two PowerShell objects:
PS> $A = [PsCustomObject]@{"A"=1; "B"=$True; "C"=$False}
PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$False}
PS> Compare-Object $A $B -Property A, B, C
A B C SideIndicator
- - - -------------
1 False False =>
1 True False <=
但是,我需要比较两个PowerShell对象的属性的存在.
However, I need to compare the existance the properties of two PowerShell objects.
这些对象将被视为相同:
These objects would be considered the same:
PS> $A = [PsCustomObject]@{"A"=1; "B"=$True; "C"=$False}
PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$True}
PS> Compare-Foo $A $B
True
这些对象将被视为不同:
These objects would be considered NOT the same:
PS> $A = [PsCustomObject]@{"A"=1; "C"=$False}
PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$False}
PS> Compare-Foo $A $B
False
有什么好方法吗?
推荐答案
我可以想到几种方法,最直接但未经实际测试的
I can think of a few ways to do this, the most straightforward but not really tested:
$A.Keys | ForEach-Object { $C = $B["$_"]; if ($C -eq "") {return $false;} }
这篇关于比较两个PsCustomObjects的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文