Get Scheduled Job Results

talkbubble-v3One of my favorite features in PowerShell 3.0 is the ability to run a PowerShell job as a scheduled task. I can easily setup a PowerShell background job to run a script but have it registered as a scheduled task. All you need is PowerShell 3.0. The job results are managed with the regular PowerShell job cmdlets. However, you will most likely end up with a large number of job results, unless you configure a smaller execution history. So the challenge is easily finding the most current job result. Here’s what I’m talking about.

Right now I have two scheduled jobs on my computer.

PS Scripts:\> get-scheduledjob

get-scheduledjob

When I run this command this will load the PSScheduledJob module. The benefit is that this also loads the ScheduledJob definition so that when I run Get-Job, I’ll see the results. Without the module loaded, Get-Job wouldn’t show anything.

get-scheduledjob-results

Right now, there are only a few results for each job. But even so, I’d like an easy way to check the most recent job. This is where you need to read the help. Originally I did not, shame on me, and I ended up with a convoluted solution. But it is actually quite easy.

PS Scripts:\> get-job "Daily Work backup" -Newest 1

get-scheduledjob-latest

I can even display more pertinent details.

PS Scripts:\> get-job "Daily Work backup" -Newest 1 | Select Name,State,*Time,Location

Name        : Daily Work Backup
State       : Completed
PSBeginTime : 3/4/2013 11:55:05 PM
PSEndTime   : 3/4/2013 11:56:37 PM
Location    : localhost

But I have multiple jobs. At first you might try something like this:

PS Scripts:\> get-scheduledjob | get-job -Newest 1 | Select Name,State,*Time,Location

But that will fail because Get-Job tries to use the ID property and we need to use the Name property. Again, reading help on the Get-Job parameters would help. Here’s a situation where we have to use ForEach-Object.

PS Scripts:\> get-scheduledjob | foreach { get-job -name $_.name -Newest 1} | Select Name,State,*Time,Location

Name        : Daily Work Backup
State       : Completed
PSBeginTime : 3/4/2013 11:55:05 PM
PSEndTime   : 3/4/2013 11:56:37 PM
Location    : localhost

Name        : Download PowerShell v3 Help
State       : Completed
PSBeginTime : 3/5/2013 6:00:02 AM
PSEndTime   : 3/5/2013 6:01:27 AM
Location    : localhost

Excellent. The only extra step I’m going to take is to add a runtime property and rename PSBeginTime and PSEndTime, which is strictly a matter of personal preference.

#Requires -version 3.0

Get-ScheduledJob |
foreach { get-job $_.name -newest 1} |
Select Name,State,
@{Name="StartTime";Expression={$_.PSBeginTime}},
@{Name="EndTime";Expression={$_.PSEndTime}},
@{Name="Runtime";Expression={($_.PSEndTime) - ($_.PSBeginTime)}},
Location

Even though this is only a one line command, because I intend to run it daily I’ll stick it in a script file to save some typing.

get-scheduledjob-script

To make it even easier, I’ll define an alias in my profile.

set-alias -name ljr -value C:\scripts\Get-ScheduledJobResult.ps1

Sure, it took a little time to work out the code in my one-line script. But I only had to type it once and now all I ever need to type is my 3 character alias!

So think about how you can be more efficient and don’t forget to read the help!!

About the Author

PowerShell.org Announcer

This is the official account for PowerShell.org and sponsor announcements.