Skip to content

Automating with Jenkins and PowerShell on Windows - Part 1

· 7 min

Take a minute think about how many PowerShell scripts you have written for yourself or your team. Countless functions and modules, helping to automate this or fix that or make your teams lives easier. You spend hours coding, writing in-line help, testing, packaging your script, distributing it to your team. All that effort, and then a lot of the time the script is forgotten about! People just go back to doing things the manual way.

I put this down to being out of sight, out of mind. Users who do not use the command line regularly will quickly forget about the amazing PowerShell-ing that you did to try and make their lives easier.

Then there are are other problems, like working out the best way to give end users permissions to use your function when they aren’t administrators. Do you give them remote desktop access to a server and only provide a PowerShell session? Setup PowerShell Web Access? Configure a restricted endpoint? I thought the point of this module was to make your life easier, not make things harder!

Enter Jenkins.

Enter Jenkins

Enter Jenkins

📢 Want to be notified when I post more like this? Follow me on Twitter: @MattHodge 📢

What is Jenkins?#

Jenkins is traditionally used by developers as a continuous integration and build tool, which provides a web interface for creating and executing both manual and scheduled jobs. The following video gives a brief introduction to Jenkins on Linux, to give you an idea of what it can do.

Using Jenkins in Operations#

Jenkins can be used to do many things for an operations team, but I will be concentrating on leveraging PowerShell to perform actions from the Jenkins server.

Anything you can think of that you can do with PowerShell, you can integrate with Jenkins to provide a user friendly interface which can be used to schedule and run jobs.

These articles will be broken up into two parts:

Installing Jenkins#

As this article is aimed at Jenkins, PowerShell and Windows, I am going to be using Windows 2012 R2 as the operating system for the Jenkins server.

Welcome to Jenkins

Welcome to Jenkins

Pretty easy right? The installer automatically configures the Jenkins service, so there is nothing left to do on the server at this stage.

Configuring Basic Security#

You will notice that you did not need to provide any credentials to login to the Jenkins interface. Lets fix that.

Configure Jenkins Security

Configure Jenkins Security

When you are logged in, you will be able to create and run jobs and manage Jenkins. When not logged in you can just see what jobs exist and view job history.

Updating Plugins and Installing the PowerShell Plugin#

Jenkins has countless plugins written by the community which extend its functionality. Once such plugin is the PowerShell Plugin, allowing us to create jobs which can run PowerShell. While we are installing this plugin, we will do a plugin update.

Installing Jenkins Plugin

Installing Jenkins Plugin

The PowerShell Plugin is now installed.

Creating a job#

Now everything is ready, we can create our first job. Our first job is going to be fairly basic - we are going to create a text file, and write a message inside the text file. This job is going to be parameterized, allowing us to pass some options into our PowerShell when we run the job. Having parameterized jobs allows you easily pass parameters to your scripts or functions, right from inside the Jenkins interface.

Jenkins does this by setting the parameters chosen in the job as environment variables. For example, if we have a Jenkins job parameter called Filename, during the Jenkins job, the PowerShell session will have an environment variable $env available to be accessed by the PowerShell the script.

At this point, the job should look like this:

Jenkins Job Creation Progress

Jenkins Job Creation Progress
Tip

Use the PowerShell ISE to write your code, then copy and paste it into the Windows PowerShell Command text box.

Terminal window
# Create Temp Directory
if (-not(Test-Path -Path 'C:\temp'))
{
New-Item -Path 'C:\temp' -ItemType directory
}
# Using the environment variables exposed by the Jenkins job
Set-Content -Path "C:\temp\$($env:Filename).txt" -Value $env:Message

Create Jenkins PowerShell Build Step

Create Jenkins PowerShell Build Step

The job is now saved and ready to go. Lets try it out.

Running a Job#

Running the Jenkins Job

Running the Jenkins Job

Jenkins Build Parameters

Jenkins Build Parameters

Jenkins Build Starting

Jenkins Build Starting

Jenkins PowerShell Script Job Output

Jenkins PowerShell Script Job Output

Success! Looks like our job was successful, lets have a look on the Jenkins servers C:\ drive to find the file and take a look what’s inside:

Jenkins PowerShell Job File Output

Jenkins PowerShell Job File Output

As you can see, we can leverage Jenkins to give our PowerShell scripts a web interface, that could be run by anyone, from anywhere!

Part 2#

Part 2 of this article has been posted. You can check it out here.