Run a Powershell script in a Jenkins pipeline

With Jenkins, you can automate a lot of CI/CD stuff pretty easily. That means, as you probably expect, that you can automatically run Powershell scripts from it without too much hassle.

To follow this guide, you will need to have:

  • A working Jenkins controller in a Linux host that has Powershell installed. You can find the official installation guide for Jenkins here. To install Powershell in your Linux host, you can follow the MS documentation available here.
  • A Powershell script that you need to run hosted in a public GitHub repository.

Before we continue, keep in mind that some cmdlets available for Windows Powershell are not going to work with the Powershell package available for Linux. For instance, things like Get-PSDrive or Get-CimInstance are not available for Linux. To avoid this, be sure to test you script before you include it in a Jenkins Pipeline.

The Powershell script

Inside a file called “script.ps1” we will have the following PS code:

#!/usr/bin/pwsh -Command
$countries = @(
    'Argentina'
    'USA'
    'China'
    'Francia'
    'Costa Rica'
    'Bolivia'
    'Canada'
)

$random = Get-Random -Maximum 7

Write-Output "Country: $($countries[$random])"

This very basic code will output the name of a random country located in an array. In a work environment, here is where you will have the code you want to run. Perhaps it can be a call to another server to trigger a script. In this case, we are using a script located in a public repository in GitHub. The first line of this code “#!/usr/bin/pwsh” tells Linux this is Powershell script.

The Jenkinsfile

A Jenkins file is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. In our case, this has a very basic structure and is also located in the same repository of the PS script:

pipeline {
  agent any
  stages {
    stage('Selected country') {
      steps {
        sh 'pwsh script.ps1'
      }
    }
  }
}

Here we only have one stage with the step to call the script.ps1 file located in the GitHub repository.

Creating the items in the Jenkins GUI

To create a new Pipeline, open up Jenkins and look for the “+ New Item” option:

Then select “Pipeline“, pick a name and select “OK“:

Next, in “Configuration: General“, go to “Pipeline” and under “Definition”, select “Pipeline script from SCM“. In the next field, “SCM“, pick “Git“.

Now, go to GitHub and copy the URL of your repository (from your branch, click in Code, and then HTTPS):

Then, paste that URL in Jenkins under “Repository URL”:

Now, look for “Branches to build” and change the “Branch Specifier” accordingly. In our case, the branch would be “Main“:

Finally, in “Script Path” enter “Jenkinsfile” and then click “Save”:

After clicking Save, Jenkins will take you to the main view of the pipeline. From there, clic in “Build Now“:

If everything worked, you will see something like this:

To check if your code executed properly, click in the circle next to the job number (#1). Once in the Build view, click on “Console Output“:

As we can see, the script picked a random country and then printed the output, exactly as it is supposed to do.

| Theme: UPortfolio