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?
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.