If you have been managing Windows servers for any length of time, you no doubt have a lot of PowerShell scripts to perform those mundane but necessary tasks you do regularly like resetting passwords, gather log data or archiving backup files.  You may have scripts that you run on a regular basis, maybe even set up a task scheduler event in an attempt to automate your PowerShell scripts.

There is a better way.

In this article, you're going to learn how to install Jenkins on Windows and run a simple PowerShell script. You will learn how to install Jenkins, set up basic security, enable the PowerShell plugin and run a job to create a simple text file with parameters that we will input.

This article is intended to be a basic introduction to Jenkins. The configuration you'll be learning isn't necessarily intended for production environments.

Prerequisites/Requirements

Most of this article is going to be a walkthrough. If you intend to follow along, be sure you have a few prerequisites in place.

The requirements to run Jenkins are so minimal, any modern computer could do it. The examples in this article will all be based on Jenkins running on:

  • Windows 10 Enterprise v.1903
  • PowerShell Core v6

Although Jenkins can run on just about any platform, I will be focusing on Windows 10 enterprise to keep things simple.

Installing Jenkins

With the prerequisites taken care of, lets install Jenkins.

Installing Jenkins is simple and straight forward. Navigate to https://jenkins.io/download and download the Long-term support (LTS) version. There are two versions available, the LTS version and the "Weekly" version. The "Weekly" version is regularly updated and can be unstable. For this article we are using the LTS version.  This is the most stable version.

As you can see from the screenshot below, there are several options to choose from. Select the Windows version to begin the download.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b596047f-b923-4881-a5f9-3b2680211f10/Untitled.png

Once you have the installer downloaded, extract the zip archive and run the installer accepting all of the defaults. Installation is simple.

Once the installation is complete, Jenkins will open a browser with the Getting Started page. This page has the initial administrator password to unlock Jenkins to continue the configuration. You can see below Jenkins is telling me to read a text file at C:\Program Files (x86)\Jenkins\secrets\initialAdminPassword.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a3a3ad1c-aa23-412a-8847-af291ade1786/Untitled.png

Navigate to the file shown open it up, copy the password shown and use it as the administrator password on the Getting Started page.

Installing Plugins

Once you log in for the first time, Jenkins will finish the final setup and then ask which plugins to install. There are a couple of choices as you can see below in the screenshot.

Selecting Install suggested plugins will install the recommended set of plugins based on the most common use scenarios. These include plugins for Authentication providers, GitHub interactions, and Java extensions. Select plugin to install allows **you to install just the ones needed from a list of all available plugins.

Highlight Install suggested plugins and click OK. This process will take a few minutes to get the initial plugins installed so now is a good time to get yourself some coffee.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1c9004dc-df8e-4666-b1e9-191baa957510/Untitled.png

Creating the Admin User

Once the plugins are installed, the next step is to create our first user. This first user is the administrator for Jenkins and will have global permissions, much like the Enterprise Administrator for Windows Server. You can see below what the Create First Admin User page will look like.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d1f4903b-50e9-4040-abe0-c61ef7fd360d/Untitled.png
From this point on, the Jenkins interface is only accessible by entering a valid username and password.

Setting the Jenkins Admin URL

The last step is to set up the URL for Jenkins service to run on. This URL is the interface for managing the Jenkins program and ideally should be set to a Fully Qualified Domain Name (FQDN). Setting it to an FQDN will allow remote access much like a website. For this article and to keep things simple, accept the default URL.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bd3c5709-15ef-481f-ae78-ef1096dc91d1/Untitled.png

Setting Up Security

With installation complete, let's configure authentication.  Navigate to the main dashboard if it is not already open. From the main dashboard, click on Manage Jenkins then click on Configure Global Security to open up the security settings as shown below.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2dcfbaf0-39e5-4122-a476-289a7b5facf0/Untitled.png

In the Configure Global Security page, you'll see a few options to choose from. For this article, we are going to use Jenkins’ own user database to keep things simple. It also a good idea untick Allow user to sign up as shown below. Having it checked will allow any user to create an account and give themselves access to Jenkins. .

Looking at the screenshot below, there are other options for authentication.

  • Delegate to servlet container - This is used if you have Docker containers already setup with Jenkins and allows authentication through the container.
  • LDAP - This option is used when you already have Active Directory that you want to use for authentication

Next, choose the Logged-in users can do anything option. In this mode, every logged-in user will have full control of Jenkins. This may not be a good idea for production as this allows any user to make changes to the settings.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/edc93824-1241-4140-bb7a-09a47982ad10/Untitled.png

Enabling the PowerShell plugin

With basic security set-up, it is time to configure the PowerShell plugin. To install the PowerShell, plugin, navigate to the main dashboard, click Manage Jenkins, then Manage Plugins as shown below.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2304b64a-b162-4e95-b3d6-eb3ec9e93509/Untitled.png

There are hundreds of plug-ins so the best way to find the PowerShell plugin is to search for it. In the filter bar, enter "PowerShell" and select it from the list. Check the bo, as shown below, to enable it. This will allow Jenkins to directly write PowerShell commands and scripts directly into the text boxes within Jenkins and allow Jenkins to invoke PowerShell without having to start a seperate PowerShell conole.

You can see below what this might look like.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fe652e80-6fd3-4b37-8575-1f9dca128a98/Untitled.png

With the plugin enabled, Jenkins is ready to run PowerShell jobs.

Creating a job

The installation of the PowerShell plugin is complete. Here comes the fun stuff.  Let's create a job.

In Jenkins, a job is defined as a series of tasks to complete.  A common scenario is a job that will start compiling a program when a developer check-ins code. Very similar to Task Manager in Windows server.

The job we are going to set up will be to check for the c:\temp directory and insert a text file in that directory with a custom message.

Jenkins has different project types, sometimes called Pipelines, that focus on specific objectives. The different types of projects will have different settings, options and parameters depending on the type of project you choose. You can find more information about the types of projects and when to use them on the Jenkins site.

From the main dashboard, click New item. For the job name, enter creating a text file. This is arbitrary but it is a good idea to label it something that makes sense.

For this article we are going to use the Freestyle project. This offers the most flexibility to create our job. Highlight the Freestyle project and scroll to the bottom and click OK.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/3e81a9c3-738c-4e2a-805d-e1cf1078ca9f/Untitled.png

This job is going to be a parameterized project. This means that we can prompt users for input and that input will be passed to the job. For example, let's say we have a job that copies log files to a specific directory. We can add a prompt to the job that will ask for a filename to include in the build when we run the job.

Check the This project is a parameterized item from the drop-down list. Click the Add Parameter drop-down and choose String Parameter. The string parameter is where you enter the message you want to include in the text file.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e1e79a17-310f-4e49-9dcc-d802a01b1033/Untitled.png

Then add a second Choice Parameter. It should look like the screen capture below. This option will give the user options to select a an item from the drop down list.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/93dfa328-3828-4a67-8164-232b6df6a7e3/Untitled.png

The job settings should now look something like the screenshot below.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a2f85a80-10d1-4bd2-9aa5-f61f2e28474e/Untitled.png

Scroll down to the Build section. Click on the small arrow and select Windows PowerShell. This lets Jenkins know we are running a PowerShell script. This is also where we enter the PowerShell script. This job is now ready. Click save.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2ecca9d7-f5e1-4c63-8753-af59ae7df16e/Untitled.png

For a sample script, let's create a file in the temp directory and add a simple message.

This is a simple script that will create temp directory under the root, if it is not already there and create a text file in that directory.

# 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
TIP:  Write your script in VSCode or another editor first then and then paste it into the box.

Running the job

Finally, you are ready to run your first job. Let’s head back to the main dashboard and you will see the Create Text File job listed in the main dashboard. See below.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/caa3593b-594e-4913-94b0-cb0620252f4f/Untitled.png

Click on the clock icon on the far right and bring up the job form.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/50acf09c-b756-46d2-9dcb-e0176e069032/Untitled.png

In the message box, enter the text you want to insert into the text file. and click build. Once the build is done, click on the console output. The console output shows the status of the job and if the job run was successful.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c6c98af9-9e75-4694-85bb-6aad59873a01/Untitled.png

Now all that is left to do is open file manager and check out the file. Navigate to the temp directory and open the text file. It will have the same text as you entered in the build message box.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/774af2c9-73f0-4007-aa91-8a881c1aead2/Untitled.png

Summary

You now have a basic understanding of Jenkins. It is a powerful tool that can be used to manage your PowerShell scripts.

Further Reading