Running Powershell scripts using Jenkins

21 / Apr / 2016 by Rahul Kumar Jaiswal 1 comments

Jenkins has been primarily used for automating jobs and tasks on Linux servers. In this blog, we will be configuring Jenkins to execute Powershell scripts on Windows. This blog will not be covering Jenkins server set up steps on Windows. You can refer to the following link for Jenkins installation.

Powershell Plugin

Jenkin’s Powershell plugin is a useful tool for running Powershell scripts on Windows servers via Jenkins.

Plugin installation

  • Login to Jenkins and navigate to Manage Jenkins > Manage Plugins.
  • Click on the Available tab and Enter PowerShell in the filter box.
  • Select the plugin showing by name PowerShell Plugin .
  • Download now and install after a restart.

The PowerShell plugin is now installed.

Now, let’s create a job on Jenkins as steps mentioned below:

  • On Jenkins interface, click New Item
  • Enter Create File for the job name. Select Freestyle project
  • Tick This build is parameterized. Expand the Add Parameter list and choose String Parameter
  • Again, Click the Add Parameter list and select Choice Parameter. Enter the options on new lines inside the Choices text box. Also, provide description for the options mentioned:

05_powershell_jenkins_jobs

  • Scroll down to the Build option and Add build step.
  • Select Windows PowerShell, inside the text box where the below Powershell script is to be supplied:

[js]
# Create Temp Directory where the files will be created.
if (-not(Test-Path -Path ‘C:\temp’))
{
New-Item -Path ‘C:\temp’ -ItemType directory
}

# Using the environment variables configured above.
Set-Content -Path "C:\temp\$($env:Filename).txt" -Value $env:Message[/js]

  • Click Save

The Job has been created.

Running the Job:

Go to the Jenkins home page and execute the job just created. Executing the job will bring a form with the options provided in the parameters specified as in the image below:

09_jenkins_powershell_job_paramaters

  • Click Build.

That’s it. Once the job is complete. A new file named Second File gets created in C:\temp with content as This is a message from Jenkins!

Executing Powershell scripts/commands remotely

The above job creates a text file on the Jenkins server itself. To setup remote Powershell scripts we first need to configure Jenkins server for remote Powershell script execution. To enable remote Windows machine in to the WS-Man trusted list on Jenkins servers. Execute the below command on Powershell window on Jenkins server. The command will add all the remote machine to the trusted list.

Set-Item WSMan:\localhost\Client\TrustedHosts *

Along with the commands, we would also need to enable remote script execution also. To enable execution of Powershell scripts remotely execute the following command in Powershell window on Jenkins server.

Set-ExecutionPolicy RemoteSigned –Force

We will have to install a new plugin named EnvInject Plugin for transferring variables e.g. passwords.

  • Login to Jenkins and navigate to Manage Jenkins > Manage Plugins
  • Click on the Available tab and Enter EnvInject in the filter box
  • Select the plugin showing by name PowerShell Plugin
  • Select Download now and install after restart

Creating a job to restart Windows time service:

  • On Jenkins interface, click New Item
  • Enter Remote Powershell scripts for the job name. Select Freestyle project
  • Tick This build is parameterized. Create following parameters
    • Type: String Parameter
    • Name: ServerIp/Hostname
    • Description: Remote machine’s IP address.
    • Type: String Parameter
    • Name: UserName
    • Type: Password Parameter
    • Name: Password
  • Now, Click Add Parameter list and select the Choice Parameter. Enter the options on new lines inside the Choices text box. Also, provide description for the options mentioned:

[js]# Configure build failure on errors on the remote machine similar to set -x on bash script
$ErrorActionPreference = ‘Stop’

# Create a PSCredential Object using the "Username" and "Password" variables created on job
$Password = $env:Password | ConvertTo-SecureString -AsPlainText -Force
$creddentials = New-Object System.Management.Automation.PSCredential -ArgumentList $env:UserName, $Password

.
# It depends on the type of job you are executing on the remote machine as to if you want to use "-ErrorAction Stop" on your Invoke-Command.
Invoke-Command -ComputerName $env:Computer -Credential $credentials -ScriptBlock {
Restart-Service -Name W32Time
}[/js]

Run the build and check the Windows logs on remote computer specified to verify if the Windows Time service has got restarted.

FOUND THIS USEFUL? SHARE IT

comments (1 “Running Powershell scripts using Jenkins”)

Leave a Reply

Your email address will not be published. Required fields are marked *