比较两个 PsCustomObject 的属性
Compare the properties of two PsCustomObjects
我知道我可以比较两个 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 <=
但是,我需要比较两个 PowerShell 对象的属性 的存在性。
这些对象将被认为是相同的:
PS> $A = [PsCustomObject]@{"A"=1; "B"=$True; "C"=$False}
PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$True}
PS> Compare-Foo $A $B
True
这些对象将被视为不同:
PS> $A = [PsCustomObject]@{"A"=1; "C"=$False}
PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$False}
PS> Compare-Foo $A $B
False
有什么好的方法吗?
我能想到几个方法来做到这一点,最直接但没有真正测试过:
$A.Keys | ForEach-Object { $C = $B["$_"]; if ($C -eq "") {return $false;} }
我知道我可以比较两个 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 <=
但是,我需要比较两个 PowerShell 对象的属性 的存在性。
这些对象将被认为是相同的:
PS> $A = [PsCustomObject]@{"A"=1; "B"=$True; "C"=$False}
PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$True}
PS> Compare-Foo $A $B
True
这些对象将被视为不同:
PS> $A = [PsCustomObject]@{"A"=1; "C"=$False}
PS> $B = [PsCustomObject]@{"A"=1; "B"=$False; "C"=$False}
PS> Compare-Foo $A $B
False
有什么好的方法吗?
我能想到几个方法来做到这一点,最直接但没有真正测试过:
$A.Keys | ForEach-Object { $C = $B["$_"]; if ($C -eq "") {return $false;} }