Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Scheduled Job Results

Posted on March 5, 2013February 21, 2014

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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!!


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d