我会为此使用比较对象。
$x = [PSCustomObject]@{
foo = 111
bar = 222
}
$y = [PSCustomObject]@{
foo = 111
bar = 222
}
$collection = [System.Collections.Arraylist]@()
[void]$collection.Add($x)
if (Compare-Object -Ref $collection -Dif $y -Property foo,bar | Where SideIndicator -eq '=>') {
[void]$collection.Add($y)
}
解释:
使用比较运算符将自定义对象与另一个对象进行比较并非易事。此解决方案比较您关心的特定属性(foo
在bar
这种情况下)。这可以简单地使用 来完成Compare-Object
,默认情况下将输出任一对象的差异。的SideIndicator
值=>
表示不同之处在于传入-Difference
参数的对象。
该[System.Collections.Arraylist]
类型用于数组,以避免+=
在增长数组时通常出现的低效。由于该.Add()
方法生成正在修改的索引的输出,因此[void]
使用强制转换来抑制该输出。
您可以对有关属性的解决方案进行动态处理。您可能不想将属性名称硬编码到Compare-Object
命令中。您可以改为执行以下操作。
$x = [PSCustomObject]@{
foo = 111
bar = 222
}
$y = [PSCustomObject]@{
foo = 111
bar = 222
}
$collection = [System.Collections.Arraylist]@()
[void]$collection.Add($x)
$properties = $collection[0] | Get-Member -MemberType NoteProperty |
Select-Object -Expand Name
if (Compare-Object -Ref $collection -Dif $y -Property $properties | Where SideIndicator -eq '=>') {
[void]$collection.Add($y)
}