简体   繁体   English

比较两个PsCustomObjects的属性

[英]Compare the properties of two PsCustomObjects

I know that I can compare the values of two PowerShell objects: 我知道我可以比较两个PowerShell对象的

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 <=

However, I need to compare the existance the properties of two PowerShell objects. 但是,我需要比较两个PowerShell对象的存在

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

Is there a good way to do this? 有什么好方法吗?

我可以想到几种方法,最直接但未经实际测试的方法:

$A.Keys | ForEach-Object { $C = $B["$_"]; if ($C -eq "") {return $false;} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM