One 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.
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.
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.
I can even display more pertinent details.
But I have multiple jobs. At first you might try something like this:
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.
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.
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.
To make it even easier, I’ll define an alias in my profile.
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!!