Scheduled Jobs in PowerShell v3

#powershell edit this page

I am really excited to write about that great feature of PowerShell I started using recently. It's all about scheduling administrative tasks, actually it's more like scheduling everything, to run when I am at home sleeping happily J

So as I have mentioned before I'm no PowerShell expert at all and I'm sure all that stuff can be done many other ways, but then this thing is working for me…

Snippets

If you have been using PowerShell ISE you might know what Snippets are, if you don't, just open powershell_ise.exe and press 'ctrl+j'. Snippets are code samples that can be added to your script easily. You can create your own snippets like that:

$code = @'

# this is my sample code

'@

New-IseSnippet -Title 'Sample' -Description 'Just a sample' -CaretOffset $code.Length -Text $code -Force

 

This will create a 'Snippets' folder at your Documents\WindowsPowerShell where your new snippet is saved for later use. Having that said let's move on to scheduled jobs.

Scheduled Jobs

Until now it was quite a pain in the butt to run PowerShell scripts as scheduled tasks, you had to specify the path to powershell.exe and then use some params to make it run the script you choose. You could even pass arguments to those scripts, but it was no real fun. So now we can do all that very easy and directly from within PowerShell.

Trigger

For starters we need to create a job trigger, to tell PowerShell when to execute out script.

$trigger = New-JobTrigger -At 22:00 –Once

 

This will create a variable containing our trigger, as you can see the script will be executed today, at 22:00 and it will be executed only once.

JobOptions

Next we can define some job options, it's not necessary though

$option = New-ScheduledJobOption –RequireNetwork -StartIfOnBattery -ContinueIfGoingOnBattery

 

Using the New-ScheduledJobOption cmdlet we can specify all kinds of different options, you might already know them from Task Scheduler.

Scriptblock

Now that we have a trigger and some options we need to define a scriptblock that is going to be executed by our scheduled job.

$sb={

param([System.IO.FileInfo]$path,[string]$string)

Add-Content -Path $path -Value $string -Force

}

 

This, obviously, is just a simple example. It will simply append a string to a file, should be enough to demo what I mean though.

Credential

Last thing we need to specify is the account that should be used to run the task.

$cred = Get-Credential

This will create a variable containing the credential you type in. We will pass this to the –Credential parameter of the next step.

RegisterScheduledJob

We have defined everything we need so we can go ahead and register the job.

Register-ScheduledJob -Name "test" -Trigger $trigger -Credential $cred -ScheduledJobOption $options -ScriptBlock $sb -ArgumentList ("c:\temp\test.txt","abcde")

 

This creates a scheduled task at \Microsoft\Windows\PowerShell\ScheduledJobs, it can be viewed using Task Scheduler or using the Get-ScheduledJob cmdlet.

If you want to specify a script file instead of a scriptblock, simply change the –ScriptBlock param and use –FilePath.

UnregisterScheduledJob

The job can be deleted in Task Scheduler or by using the Unregister-ScheduledJob cmdlet.

 

 

I hope you will find this feature as exciting as I do, I'll provide you with the code snipped so you can easily use 'crtl+j' to create a task the next time. Just copy the ps1xml file to your Documents\WindowsPowerShell\Snippets folder and enjoy. J

Scheduled Job.snippets.ps1xml (1.35 kb) 

 

So long, have a nice weekend!

tom