Are you transitioning from Windows system administrator to DevOps engineer and want to continue using your PowerShell skills? One way to do this is to set up Jenkins to use PowerShell.
When setting up a Jenkins job, you can execute Linux shell or Windows bash commands to automate tasks. However, the preset does not include executing PowerShell commands.
Fortunately, there is a way to make PowerShell appear as an option in a Jenkins job. This guide describes the steps to execute PowerShell commands as part of the Jenkins build step.
Please follow the steps below to start using PowerShell in Jenkins job automation:
Step 1: Install the Jenkins PowerShell plugin
- Log in to your Jenkins server and click Manage Jenkins => Plugins
- To install the Jenkins PowerShell plug-in, click the “Available plug-ins” node in the left pane. Then, type “powershell” into the search bar, and finally select the box next to the PowerShell plug-in and click “Install.”
- Finally, wait for the plugin to install, scroll down, and click “Back to Home.”
Step 2: Test the Jenkins PowerShell plugin
After completing the first step of setting up Jenkins to use PowerShell, let’s test it out. To achieve this, we will set up a Jenkins FreeStyle job and add a PowerShell command.
- To create a FreeStyle project, click + New Project or Create Job + from the Jenkins dashboard.
- After that, name the Jenkins job, select the Freestyle project, and click OK. Jenkins will build the job and open it for editing.
- Scroll down to the “Build Steps” section of the operation and click the “New Build Step” drop-down list.
- As the screenshot below confirms, PowerShell is now available as an option! Select it.
- Finally, enter a simple command in the PowerShell field – Get-ChildItem /etc/ – and click save button.
6. Return to the Jenkins project, click the “Build Now” button, and then select “Schedule Build”.
The build will fail. To view build details, scroll down to Build History and click on one of the operations.
Reviewing the job’s build console output can reveal why it failed. The job attempts to execute a PowerShell (pwsh) command, but PowerShell is not installed on the Jenkins server.
This is why the job fails. To solve this problem, we need to install PowerShell on the Jenkins server.
The next section explains the steps to achieve this.
Step 3: Install PowerShell on the Jenkins server
The final step in setting up Jenkins to use PowerShell is to install PowerShell on the Jenkins host server.
If Jenkins is executed directly on the Ubuntu server, PowerShell must be installed on the server. However, if you start Jenkins as a Docker container, you must install PowerShell on the container.
In my case, my Jenkins server will run as a Docker container, so I will install PowerShell on the container. Here’s a step-by-step walkthrough:
- Log in to the Jenkins Docker container as root by executing the following “Docker exec” command:
sudo docker ps #returns the container ID sudo docker exec -u 0 -it 977a52341f9a bash #logs into the container as root
The Docker exec command requires a container ID. The “docker ps” command returns this information.
The “-u 0” parameter allows you to log in as root. On the other hand, “-it” lets you log in “interactively”.
The string 977a52341f9a is the ID of my Jenkins container, and adding “bash” at the end will turn on the BASH prompt character. This is the output of the command.
The part of the screenshot I marked (1) shows that I’m logged into my Docker container – 977a52341f9a is my container’s ID. Also, the # symbol means I’m logged in as root.
- To install PowerShell, we first need to determine the Linux distribution executing on the Docker container. To obtain this information, execute the following command:
cat /etc/os-release
This command confirms that my container is running Debian Linux Distro.
This information will help me identify the Microsoft script used to install PowerShell. So, since the distribution of my container is Dabien, I will use the Installing on Debian script.
Here is the modified script – I removed “sudo” as we don’t need it on the container – we are logged in as root!
################################### # Prerequisites # Update the list of packages apt-get update # Install pre-requisite packages. apt-get install -y wget # Download the PowerShell package file wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.0/powershell_7.4.0-1.deb_amd64.deb ################################### # Install the PowerShell package dpkg -i powershell_7.4.0-1.deb_amd64.deb # Resolve missing dependencies and finish the install (if necessary) apt-get install -f # Delete the downloaded package file rm powershell_7.4.0-1.deb_amd64.deb # Start PowerShell pwsh
Before proceeding, execute the “apt update” command to update the package manager.
Copy the above script, paste it into the container’s console, and press Enter.
When you execute this command, you will be prompted to confirm the installation. Enter Y and press Enter.
When the script finishes running, it will open the PowerShell console to confirm that we have successfully installed PowerShell on the Docker container.
- To exit PowerShell and return to the container’s shell, execute the “exit” command. Additionally, to exit the container, execute “exit” again.
Step 4: Rebuild the Jenkins job and test
Now that we have PowerShell installed on the Jenkins server (a Docker container in my case), we can rebuild the job and see if it runs successfully.
- Log in to the Jenkins server through a browser and open the PowerShell project.
- After that, click the “Build Now” button and select “Schedule Build.”
- When the build job has finished running, scroll down to the Build History section. The build should now be successful. Click on the last build job.
- Finally, click on “Console Output”
The details of the build operation confirm that the “Get-ChildItem” command ran successfully!
Although the commands we used for this demonstration are simple, it demonstrates that after setting up Jenkins to use PowerShell, you can use PowerShell commands to automate Jenkins jobs.
We hope you’ll take your DevOps skills to the next level by reading this guide. We’d love to hear your thoughts.
Use the comment form at the end of this page to share your thoughts or ask questions about this topic or any DevOps issue.
Other useful resources
- Jenkins PowerShell plugin
- What is the difference between Freestyle and Pipeline in Jenkins?
- Install on Debian 10 or 11 via package repository
- How to install Jenkins on Ubuntu using Docker containers
2 Comments
Pingback: How to configure Jenkins using PowerShell – Paxton Willson
Pingback: How to configure Jenkins using PowerShell – Mary Ashley