I'm starting to use compare-object more and more, and one thing I noticed, is that you can compare 2 objects based on multiple attributes. here is how it is constructed...
Compare-Object -ReferenceObject $object1 -DifferenceObject $object2 -Property a,b,c,d,eIf a,b,c and d are the same, but e is different, compare object will return a difference. In the following example, I use "-eq $null" as a check because by default compare-object returns $null if the objects are the same.
#create an array of objects to check against
$collection = @()
foreach ($entry in ("aaaaa","bbbbb","ccccc","ddddd")){
$store = "" | select "a","b","c","d","e"
$store.a = $entry*1
$store.b = $entry*2
$store.c = $entry*3
$store.d = $entry*4
$store.e = $entry*5
$collection += $store
}
#create an object similar to those in the array
$object = "" | select "a","b","c","d","e"
#change $text to one of these to get a different result - "aaaaa","bbbbb","ccccc","ddddd"
$text= "ccccc"
#the $text*2 etc makes sure all the variables in the compare are different
$object.a = $text*1
$object.b = $text*2
$object.c = $text*3
$object.d = $text*4
$object.e = $text*5
#loop the array and output all that are the same
foreach ($entry in $collection){
if ((Compare-Object -ReferenceObject $entry -DifferenceObject $object -Property a,b,c,d,e) -eq $null){
"this object is the same..."
$entry
}
}
Comments
Post a Comment