Compare the properties of two PsCustomObjects

你离开我真会死。 提交于 2019-12-12 03:48:25

问题


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

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

Is there a good way to do this?


回答1:


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;} }


来源:https://stackoverflow.com/questions/40536621/compare-the-properties-of-two-pscustomobjects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!