Powershell find unique value

Powershell

I have an array of the following PSObject:

$object = New-Object -TypeName PsObject
$object | Add-Member –MemberType NoteProperty –Name firstName –Value $_.firstname
$object | Add-Member –MemberType NoteProperty –Name SSO –Value $_.CPR
$object | Add-Member –MemberType NoteProperty –Name WorkContract –Value $_.WorkContract
$object | Add-Member –MemberType NoteProperty –Name numerator –Value $_.numerator
$object | Add-Member –MemberType NoteProperty –Name entryDate –Value $_.entryDate

For example:

Firstname: John
SSO: 111111
Workcontract: 00
Enumerator: 50
EntryDate: 31-02-2008

Firstname: John
SSO: 111111
Workcontract: 02
Enumerator: 67
EntryDate: 31-02-2009

Firstname: Joe
SSO: 222222
Workcontract: 08
Enumerator: 45
EntryDate: 31-02-2001

I need to be able to return only one PSObject per SSO. (create an array only containing unique SSO)
Based on priority:
1.      Workcontract: (lowest first (eg. 00)) (if equal - priority 2)
2.      Enumerator: (higest number) (if equal - priority 3)
3.      EntryDate:  (oldest date first)

Hope I make my self clear ;)

Mike
LVL 1
mikeydkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Note that your test data can't be used - there's no Gregorian calendar with a February 31st.
And you missed some important information when it comes to sorting:
What types are the properties "Workcontract" and "Enumerator" supposed to be - integer or string?
And what about the EntryDate: is that a string or already a DateTime object?
Anyway, that should do it, assuming your objects are in an array named "$Array", and currently all of type String.
WorkContract will be sorted as string (because of the leading zero), Enumerator as UInt64, EntryDate as DateTime.
$Array | Group-Object -Property SSO | ForEach-Object {
	$_.Group | Sort-Object -Property `
		@{Expression={$_.Workcontract}; Descending=$false},
		@{Expression={[UInt64]$_.Enumerator}; Descending=$true},
		@{Expression={[DateTime]::ParseExact($_.EntryDate, "dd'-'MM'-'yyyy", $Null)}; Descending=$false} |
	Select-Object -First 1
}

Open in new window

For testing, you can just drop the "| <CRLF> Select-Object -First 1" to see the fully ordered version; the first of each SSO group will end up in the script above:
$Objects | Group-Object -Property SSO | ForEach-Object {
	$_.Group | Sort-Object -Property `
		@{Expression={$_.Workcontract}; Descending=$false},
		@{Expression={[UInt64]$_.Enumerator}; Descending=$true},
		@{Expression={[DateTime]::ParseExact($_.EntryDate, "dd'-'MM'-'yyyy", $Null)}; Descending=$false}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikeydkAuthor Commented:
oBdA> Thanks ;)

Any cool way to ignore an error in  line "@{Expression={[DateTime]::ParseExact($_.EntryDate, "dd'-'MM'-'yyyy", $Null)}; Descending=$false}" if $_.EntryDate is empty or null?

Thanks in advance
0
oBdACommented:
Well, how it's supposed to be sorted in this case (very old - high priority, very far in the future - low priority)?
0
mikeydkAuthor Commented:
High priority ;)
0
oBdACommented:
$Objects | Group-Object -Property SSO | ForEach-Object {
	$_.Group | Sort-Object -Property `
		@{Expression={$_.Workcontract}; Descending=$false},
		@{Expression={[UInt64]$_.Enumerator}; Descending=$true},
		@{Expression={Try {[DateTime]::ParseExact($_.EntryDate, "dd'-'MM'-'yyyy", $Null)} Catch {[DateTime]::MinValue}}; Descending=$false} |
	Select-Object -First 1
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.