问题
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