Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to see the results of scheduled tasks.
Microsoft Scripting Guy, Ed Wilson, is here. It is snack time. For me, a perfect snack is a piece of biscotti and a cup of tea. Today, I am having such a snack—the tea is a nice green tea with a bit of jasmine flower in it. Lovely.
Something else that is lovely is using the cmdlets from Scheduled Tasks module. This module contains a number of very helpful cmdlets.
Note For a great overview of the Scheduled Tasks module, see Windows 8.1 and the Land of Forgotten Modules: Part 5.
Working with scheduled tasks
The Task Scheduler is very sophisticated. It is so sophisticated that many applications use it to run things at various times. If I want to get an idea of the tasks that are ready to run, it is going to require a lot of clicking in the GUI. But with Windows PowerShell, it is a piece of cake. I type a command such as the following—and voila! Lots of information appears.
Get-ScheduledTask | where state -EQ 'ready'
The output is shown here:
There are lots of scheduled tasks that are actually ready to run. What I if I want to find out the results of the last time the task ran? I know where to go in the GUI, but what about using Windows PowerShell? I look at cmdlets from the Scheduled Tasks module, and I see some things that make sense. Here are the results:
PS C:\> Get-Command -Module ScheduledTasks -Verb get
CommandType Name ModuleName
----------- ---- ----------
Function Get-ClusteredScheduledTask ScheduledTasks
Function Get-ScheduledTask ScheduledTasks
Function Get-ScheduledTaskInfo ScheduledTasks
I suspect that I can use Get-ScheduledTaskInfo. So I give it a try. I know one of the scheduled tasks is Consistency.
Dude. How rude! I mean, I KNOW the task name is Consistency, I just wrote a blog post about it ( see Run a Scheduled Task to Check DSC). I use the Get-ScheduledTask cmdlet to prove this:
PS C:\> Get-ScheduledTask -TaskName consistency
TaskPath TaskName State
-------- -------- -----
\Microsoft\Windows\Desired State Configurat... Consistency Ready
Yep. It is Consistency alright. What if I use the –TaskPath parameter? As shown here, it is now asking for the name. Dude...
I type the task name at the prompt, and finally I get the results of the last run. This is shown here:
If I am going to go to all that trouble, I may as well just pipe Get-ScheduledTask in the first place:
Get-ScheduledTask consistency | Get-ScheduledTaskInfo
Cool. If I can pipe the results from Get-ScheduledTask to Get-ScheduledTaskInfo, I can make myself a report by piping the results to Export-CSV:
Get-ScheduledTask | where state -EQ 'ready' | Get-ScheduledTaskInfo |
Export-Csv -NoTypeInformation -Path C:\fso\scheduledTasksResults.csv
Here is the output in Excel:
Sweet. That was easy.
That is all there is to using Windows PowerShell to find the results of scheduled tasks. Scheduled Tasks week will continue tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy