Execute PowerShell scripts in Jenkins Jobs

PowerShell is now a 10 year old scripting technology. It is a must know for everyone who is working as sysadmin for one of the Microsoft server products whether its Windows server, SharePoint, Exchange, System Center etc. In fact, its part of Microsoft Server stack basic requirements, and no product is passed to release if it does not match this requirement. So its supported by all of the Microsoft Server products in one way or the other. It’s therefore naturally to assume that build admins need to use it during one of their builds. In this blog post, we’ll cover how to execute PowerShell related commands or scripts in a Jenkins build.

First, we need to add ‘PowerShell Plugin’ from Manage Jenkins -> Manage Plugins -> Available section:

select-and-install-powershell-plugin

After this, let’s create a basic freestyle Job inside Jenkins:

Create a empty free stypleproject for PowerShell.JPG
Create a empty free stypleproject for PowerShell

You do not have to create a new job for this. Once plugin is installed, you can simply skip to build step in existing job and add a build step as explained next.

Once its created, go to build section and select add build step. From list of options presented, select ‘Windows PowerShell’:

Select 'Windows PowerShell' from list of build steps.JPG
Select ‘Windows PowerShell’ from list of build steps

We have added two simple commands to our build step:

Write-Output "The hostname of this machine is $env:ComputerName"
Write-Output "The PowerShell version details on this machine are as follows:"
$PSVersionTable

Now let’s save and build the job. Note that we have Jenkins installed on a Windows machine for this post’s purposes. PowerShell for Linux versions is still in alpha stage and if you want to install PowerShell on a linux distro, check this post.

If everything is configured successfully, you should an output like this:

Started by user admin
Building in workspace C:\Program Files (x86)\Jenkins\workspace\Just PowerShell
[Just PowerShell] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Windows\TEMP\hudson5402219922441638197.ps1'"
The hostname of this machine is JENKINS
The PowerShell version details on this machine are as follows:

Name                           Value                                           
----                           -----                                           
PSVersion                      5.0.10586.117                                   
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         
BuildVersion                   10.0.10586.117                                  
CLRVersion                     4.0.30319.42000                                 
WSManStackVersion              3.0                                             
PSRemotingProtocolVersion      2.3                                             
SerializationVersion           1.1.0.1                                         


Finished: SUCCESS

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s