Jenkins

Install and Use the PowerShell Jenkins plugin

PowerShell is a Microsoft task automation and configuration management framework consisting of a command-line shell and associated scripting language.

Powershell is based on the .NET framework and aims to provide a simple, interactive, and objected-oriented scripting language that can be extended beyond the scope of the command line. PowerShell enables administrators to perform administrative tasks on local and remote Windows systems.

It is a powerful tool for essential to complex automation tasks within and beyond the Windows filesystem. It benefits systems administrators who manage multiple systems, allowing them to perform and automate routine tasks remotely.

When working in Jenkins, you may encounter instances where you must execute PowerShell commands without using an external script.

In this tutorial, you will learn how to install and configure the PowerShell plugin in Jenkins, allowing you to execute PowerShell commands in a text box.

Requirements

To follow along with this tutorial, you will need to have the following:

  1. A Jenkins controller installed on your system.
  2. PowerShell interpreter setup on your system.
  3. The necessary permissions to install and configure plugins in Jenkins.

With the above requirements met, we can proceed.

Installing the PowerShell Plugin

The first step is to install the PowerShell plugin on our Jenkins controller.

Start by logging into Jenkins Dashboard -> Manage Jenkins -> Manage Plugins.

Select Available Plugins and search for “PowerShell.”

Select the PowerShell plugin and click “Download now and install after restart.”

Once Jenkins has restarted, log back in and configure the PowerShell plugin.

Jenkins Configure PowerShell

Once we have PowerShell installed, we need to configure and allow Jenkins to run PowerShell commands.

Open the Jenkins Dashboard -> Manage Jenkins -> Global Configuration Tool.

Navigate to the PowerShell section and click “Add PowerShell.”

This will allow you to add a name for the PowerShell installation for both Windows and Linux.

Once configured, click Save to apply the changes and allow Jenkins to run PowerShell commands.

Jenkins Run PowerShell Script

In a Jenkins pipeline, you can run a PowerShell command using either a bat or sh section.

An example Jenkinsfile is as shown:

pipeline {
    agent any

    stages {
       stage('Version') {
           steps {
                bat 'powershell $PSVersionTable '
           }
       }
    }
}

The above pipeline will get the current PowerShell version on a Windows Jenkins controller.

If you are on Linux, we need to use the sh section as:

pipeline {
      agent any

      stages {
           stage('Version') {
                steps {
                     sh 'pwsh $PSVersionTable '
                }
           }
      }
}

Once you run the pipeline, you should get the current PowerShell version.

Conclusion

In this article, you learned how to install and use the PowerShell plugin in Jenkins learned how to run PowerShell scripts in Jenkins pipeline.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list