Migrating Powershell Scheduled Tasks from Windows Task Scheduler to Jenkins

Why migrate to Jenkins?

As with all technology it has to do with your work flow. Jenkins provides a nice generic dashboard that is easy to monitor, allows me to give out access by project, and integrates nicely into SVN and GIT. My general goal is to give the developers more control over the environment (Expand drives, Spin up virtual machines, migrate virtual machines to different hosts, run maintenance tasks, etc.)

If you have never used Jenkins or want to know how to set it up in windows: Matthew Hodgkins’ blog wrote a really great 2 part blog entry on getting started with Jenkins and Powershell. So I won’t cover the basic configuration as his blog entry covers all of the basics and then some. I will assume you have at least finished part 1 of the blog. That way I can jump straight to how to start migrating your scheduled tasks to Jenkins. I am going to cover script error handling gotchas and how to exit scripts with Jenkins. That won’t be schedule task specific but a something  you should be aware of as you migrate your scripts to the new platform, and may be a style change for Jenkins scripts.

Creating a Scheduled Task:

For this example create a scheduled task:

First Select a “New Item” to create a new project:


We will be doing a “FreeStyle project” that I am calling “Run every 5 minutes”:


At this time we will be making an extremely simple scheduled task. That means we can ignore most of the options scrolling down to “Build Triggers” and selecting “Build periodically”.

In the text box we will be entering:

H/5 * * * *


The scheduling is built around the cronjob timing system. Clicking the question mark offers an excellent drop down to give a full explanation the scheduling of Jenkin Jobs. For now it is enough to know we scheduled the task to run every 5 minutes.

A quick note: The system doesn’t evaluate your scheduled task schedule until you click off of the text window. So it won’t show the run explanation until you click away.

For this simple task we will be outputting text to a file:

echo “write a line” >> C:\temp\write_to_file.txt



Since our script outputs to a local directory. We’ll need to add the C:\temp folder on the Jenkins server. For now make sure that the NTFS permissions are set so “Everyone” can write to the folder:


Hit save in Jenkins. Return to the Dashboard by using the navigation bar on the right:



You can manually launch the job, or wait the 5 minutes and confirm the script worked.

To manually launch:



Now that we have finished the scheduled task. Lets talk about Jenkins error handling for a second.

Jenkins Error Handling Gotchas

Go back to the NTFS permissions on the C:\temp folder and Deny Everyone and hit apply:


Wait for or force the scheduled task to run. After it runs the task succeeded:

success Lets take a look at what the scripts output via the terminal. Click on the “Run Every 5 Minutes” link and look for where it says “Last build” Under Permalinks. Click on “Last Build”


The terminal output shows the error:


The issue has to do with how Jenkins detects the script exit. A success is read as:

Exit 0

A failure:

Exit 1

This means anywhere you would return a True or False value to exit your script. Such as:

try {

echo “Folder is blocked so I should return false” >> C:\temp\write_to_file.txt
return $true

} catch {

return $false


You would need to use an exit code.

Enter your code as:

Try {
echo “write a line” >> C:\temp\write_to_file.txt
exit 0
} catch {
exit 1

Leads to the expected failure message:

After reverting the c:\temp NTFS permissions to allow everyone:



Thanks for reading.

Sharing is caring!

Leave your comment

two × three =