@pmoncho said in Powershell split and sort date/time:
@obsolesce said in Powershell split and sort date/time:
@pmoncho said in Powershell split and sort date/time:
@pmoncho
Small update - Changed code as I realized I had to build an additional array outside the foreach loop.
$taskserver = "RemoteSystem"
$tNames = "*Location*"
$taskList = (Get-ScheduledTask -CimSession $taskserver -TaskName $tNames)
$_taskListing = foreach ($task in $taskList){
$tName = $task.TaskName
$tNameXML = ($tName+".xml")
$tPath = $task.TaskPath
Get-ScheduledTaskInfo -CimSession $taskserver -TaskPath $tPath -TaskName $tName | Select-Object TaskName, NextRunTime
}
#check to see the array is sorting at least by TaskName
$_taskListing | Sort TaskName -Descending
on the last line, have you tried sorting by the next run time?
$_taskListing | Sort NextRunTime
I just got back here and yes, I did that and it does sort it correctly. I forgot I had it sorted by TaskName, changed it and it worked fine (after setting it as an array).
I would still like to figure out how to separate the Date and Time though. That way I can use the most recent start time and add 1 minute to it for the new task that will be created.
So, while I have the sort fixed, how the heck do I split it?
My quick example output:
PS C:\Windows\system32> $_taskListing
TaskName NextRunTime
-------- -----------
MicrosoftEdgeUpdateTaskMachineCore 10/13/2021 8:09:09 PM
MicrosoftEdgeUpdateTaskMachineUA 10/13/2021 12:39:39 PM
Microsoft Compatibility Appraiser 10/14/2021 3:14:14 AM
Microsoft-Windows-DiskDiagnosticDataCollector
Microsoft-Windows-DiskDiagnosticResolver
If you get the type of one of the objects your script is outputting:
($_taskListing.NextRunTime)[0].getType()
The result is:
PS C:\Windows\system32> ($_taskListing.NextRunTime)[0].getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True DateTime System.ValueType
Because it's a DateTime object, it's easy to manipulate.
You can easily add one minute to it using one of the built in methods:
($_taskListing.NextRunTime)[0].AddMinutes(1)
PS C:\Windows\system32> ($_taskListing.NextRunTime)[0].AddMinutes(1)
Wednesday, October 13, 2021 8:10:09 PM
You can do this part how it works best with your workflow.