Comparing objects using multiple properties ?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
Tony67
Posts: 4
Joined: Thu Sep 06, 2018 1:20 pm

Comparing objects using multiple properties ?

Post by Tony67 »

I have an array of objects. Each object has the following properties: upn, displayname, ipaddress, datetime, clientappused, useragent, succeeded.

I am looking to compare the objects on upn, displayname, clientappused and useragent and filter our duplicates of these.

eg obj1
upn:alpha.bravo@delta.com
displayname:Alpha Bravo
ipaddress:1.1.1.1
datetime:1/01/2020 01:00
clientappused:IMAP
useragent:Agent
succeeded:true

obj2
upn:bravo.charlie@delta.com
displayname:Bravo Charlie
ipaddress:1.1.1.2
datetime:1/01/2020 01:00
clientappused:IMAP
useragent:Agent
succeeded:true

obj3
upn:alpha.bravo@delta.com
displayname:Alpha Bravo
ipaddress:1.1.1.3
datetime:2/01/2020 02:00
clientappused:IMAP
useragent:Agent
succeeded:false

obj4
upn:echo.foxtrot@delta.com
displayname:Echo Foxtrot
ipaddress:1.1.1.4
datetime:2/01/2020 02:00
clientappused:MAPI
useragent:Agent2
succeeded:true

The results I require are objects 1,2 and 4. Object 3 is to be skipped because obj1 has the same upn,displayname,clientappused and useragent values. I require the FULL object (all properties) in the results.

I know I can compare the objects with multiple properties (compare-object $obj1 $obj2 -property $properties) but am unsure how to go about looping through the objects and adding to the results.

Any help greatly appreciated.

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Comparing objects using multiple properties ?

Post by jvierra »

You can compare object collections with Compare-Object.

Start by reading the help very carefully and reviewing the examples.

help compare-object -online

Tony67
Posts: 4
Joined: Thu Sep 06, 2018 1:20 pm

Re: Comparing objects using multiple properties ?

Post by Tony67 »

Sorry - don't see how that helps. Maybe my logic is wrong to begin with.

I was planning on adding the first object to my results. Then loop through the objects comparing to the first object in results. If I find a match, break out of the loop and start at the next object. If I don't find a match, add the object to results. The issue I seem to be having is the change of 'results' causes errors in my loop.

jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Comparing objects using multiple properties ?

Post by jvierra »

That is not how PowerShell works. First you have to better define why you need to do this and then, perhaps, we can show you how accomplish your task with PowerShell.

Do you understand that Compare-Object does exactly what you have described? I can compare all objects in two collections and show what matches, what doesn't match as two separate outputs. That is the technical meaning of "compare" We can compare only two ways; equal and not equal. We can get results that are equal or we can get results that are not equal.
The other method of testing is called "contains/notcontains" or "in/notin". This is a set membership operation. Is it possible that you are asking how to determine membership?

Locked