i have the following psobject (imported from csv) this is the original,
Computer Name : CompName
Date : 10/26/2020
7/1/2021
3/6/2019
7/13/2022
3/6/2019
2/7/2021
10/13/2020
2/7/2021
2/7/2021
7/1/2021
10/13/2020
IP Address : 127.0.0.1
OS : Win10 10.0.17763.1282 (1809)
CPU : 2600 MHz Core i5-6500T
Last Report Time : Wed, 1 Aug 2022 09:54:51 0000
Computer Name : CompName2
Date : 2/10/2021
6/24/2021
3/6/2019
3/6/2019
2/12/2021
2/9/2021
2/9/2021
6/24/2021
2/10/2021
IP Address : 127.0.0.2
OS : Win10 10.0.17763.973 (1809)
CPU : 2700 MHz Core i5-6500T
Last Report Time : Wed, 1 Aug 2022 11:38:34 0000
I want to sort the date and get the last date in single object. meaning it will be somthing like that this is the last wanted.
Computer Name : CompName
Date : 7/13/2022
IP Address : 127.0.0.1
OS : Win10 10.0.17763.1282 (1809)
CPU : 2600 MHz Core i5-6500T
Last Report Time : Wed, 1 Aug 2022 09:54:51 0000
Computer Name : CompName2
Date : 6/24/2021
IP Address : 127.0.0.2
OS : Win10 10.0.17763.973 (1809)
CPU : 2700 MHz Core i5-6500T
Last Report Time : Wed, 1 Aug 2022 11:38:34 0000
any ideas?
CodePudding user response:
Let's start by looking at how we can do it for a single value first.
Import-Csv
will produce a single multi-line string:
$dates = @'
10/26/2020
7/1/2021
3/6/2019
7/13/2022
3/6/2019
2/7/2021
10/13/2020
2/7/2021
2/7/2021
7/1/2021
10/13/2020
'@
Before we can meaningfully sort the individual date strings, we need to split them into multiple strings:
$dates -split '\r?\n'
After which we can ask Sort-Object
to treat each string as a [datetime]
value to get the correct sort order:
$datesSorted = $dates -split '\r?\n' |Sort-Object { [datetime]::ParseExact($_, 'd/M/yyyy', $null) } -Descending
And then finally grab just the first value to get the largest/newest:
$datesSorted |Select-Object -First 1
Now that we know how to get the correct line from a single Date
property, we can put it all together and modify each object with a ForEach-Object
statement:
$csvData |ForEach-Object {
$_.Date = $_.Date -split '\r?\n' |Sort-Object {[datetime]::ParseExact($_, 'd/M/yyyy', $null)} -Descending |Select -First 1
}
The objects stored in $csvData
will now all have had their Date
property value modified as desired
CodePudding user response:
I just re-read your post. What is your import-csv CMDLET look like? CSV by default do not import multi-dimensional arrays. How is the date column formatted in the CSV file? You also have two objects so I'm going to assume this is an array we are dealing with.
Original Response:
This will work on a single object or an array as long as the date property is an array of strings.
This will keep the array of dates in case you need it for something else and adds one additional property "RecentDate"
$simpleArray=$simpleArray | Select *, @{N="RecentDate";e={$_.Date | sort | select -last 1}}
$dates1 = "10/26/2020
7/1/2021
3/6/2019
7/13/2022
3/6/2019
2/7/2021
10/13/2020
2/7/2021
2/7/2021
7/1/2021
10/13/2020" -split("`n")
$dates1=$dates1.trim()
$dates2 = "2/10/2021
6/24/2021
3/6/2019
3/6/2019
2/12/2021
2/9/2021
2/9/2021
6/24/2021
2/10/2021" -split("`n")
$dates2=$dates2.trim()
$obj1 = [pscustomobject]@{'Computer name'="CompName"
Date=$dates1
'IP Address'="127.0.0.2"
OS="Win10 10.0.17763.973 (1809)"
CPU="2700 MHz Core i5-6500T"
'Last Report Time'="Wed, 1 Aug 2022 11:38:34 0000"
}
$obj2 = [pscustomobject]@{'Computer name'="CompName2"
Date=$dates2
'IP Address'="127.0.0.2"
OS="Win10 10.0.17763.1282 (1809)"
CPU="2700 MHz Core i5-6500T"
'Last Report Time'="Wed, 1 Aug 2022 09:54:51 0000"
}
$simpleArray = New-Object -TypeName 'System.Collections.ArrayList';
$null=$simpleArray.add($obj1)
$null=$simpleArray.add($obj2)
Output:
Computer name : CompName
Date : {10/26/2020, 7/1/2021, 3/6/2019, 7/13/2022...}
IP Address : 127.0.0.2
OS : Win10 10.0.17763.973 (1809)
CPU : 2700 MHz Core i5-6500T
Last Report Time : Wed, 1 Aug 2022 11:38:34 0000
RecentDate : 7/13/2022
Computer name : CompName2
Date : {2/10/2021, 6/24/2021, 3/6/2019, 3/6/2019...}
IP Address : 127.0.0.2
OS : Win10 10.0.17763.1282 (1809)
CPU : 2700 MHz Core i5-6500T
Last Report Time : Wed, 1 Aug 2022 09:54:51 0000
RecentDate : 6/24/2021