craig
craig

Reputation: 26262

Compare the properties of two PsCustomObjects

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?

Upvotes: 1

Views: 3171

Answers (1)

Austin T French
Austin T French

Reputation: 5131

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

Upvotes: 1

Related Questions