Powershell split and sort date/time
-
Powershell script to get a list of Scheduled Tasks on remote server, get the task info and then sort by NextRunTime then Name.
Reason is, I will have to create these same tasks in the future and want to offset the new tasks to start 1 minute after the last.
$taskserver = "RemoteSystem" $tNames = "*Location*" $taskList = (Get-ScheduledTask -CimSession $taskserver -TaskName $tNames) foreach ($task in $taskList){ $tName = $task.TaskName $tPath = $task.TaskPath Get-ScheduledTaskInfo -CimSession $taskserver -TaskPath $tPath -TaskName $tName | Select-Object TaskName, NextRunTime }
Results in two columns:
TaskName NextRunTime LOC TUL LAB 10/13/2021 7:01:01 PM LOC TUL RT 10/13/2021 7:01:01 PM LOC TPL LAB 10/13/2021 7:08:03 AM LOC TPL RT 10/13/2021 7:00:25 PM LOC RTL LAB 10/13/2021 7:06:06 AM LOC RTL RT 10/13/2021 6:58:55 AM LOC JRT LAB 10/13/2021 8:00:00 PM
How can I sort by the date / time column? I am stumped.
I looked up [datetime]::ParseExtract and Calculated properties but over my head when trying to use them.
-
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
-
@pmoncho said in Powershell split and sort date/time:
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
-
@obsolesce said in Powershell split and sort date/time:
@pmoncho said in Powershell split and sort date/time:
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?
-
@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:
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.
-
That is pretty cool. I will give that a try this morning.