How can I sort PSObjects and remove duplicates with the oldest date?
hi,
i have collection of psobjects which contains several values. goal produce list of unique apptoken objects kicker is if duplicate object exists, need keep newest 1 (which can determined dtcreateddate)
here's have tried (unsuccessfully)
$tokentable = $tokentable|sort-object -property @{expression="dtcreateddate";descending=$true} $tokentable = $tokentable|sort-object -property @{expression="apptoken";descending=$true} |get-unique -asstring i can commands work individually, not together.
i tried 1 liner:
$tokentable = $tokentable|sort-object -property@{expression="apptoken";descending=$true},@{expression="dtcreateddate";descending=$false}|get-unique -asstring here's object looks like:
typename: selected.system.management.automation.pscustomobject
name membertype definition
---- ---------- ----------
equals method bool equals(system.object obj)
gethashcode method int gethashcode()
gettype method type gettype()
tostring method string tostring()
applicationdisplayname noteproperty system.string applicationdisplayname=website
apptoken noteproperty system.string apptoken=/dty
databasename noteproperty system.string databasename=db1
dbserver noteproperty system.string dbserver=dbsql1
dtcreateddate noteproperty system.datetime dtcreateddate=10/03/11 6:24:13 am
iscustom noteproperty system.boolean iscustom=false
isenabled noteproperty system.boolean isenabled=false
try this, seemed work ok using clixml posted:
$results = @() $apptokens = $tokentable | select apptoken -unique foreach ($token in $apptokens) { $tokentable | ?{$_.apptoken -match $token.apptoken} | sort dtcreateddate -desc | select -first 1 | %{$results += $_} } the $apptokens line grabs unique apptoken values. it cycles through them, gets instances of particular apptoken, picks newest one, , adds whole object $results array.
i hope post has helped!
Windows Server > Windows PowerShell
Comments
Post a Comment