How to use WinRM & PowerShell with Jenkins


Jenkins is a powerfull continuous integration tools java based that exists for both Windows and Linux servers. It’s maybe one of the most used on premise. If you do CI for you devs, you should take a look at their web site and shout out a vm to start play with it.

But some thing bothered me, if you have a Linux based Jenkins and want to automated build process on Windows platforms how can we do, without using a Jenkins Window slaves plugin ? Let me explain you what i did in order to have Jenkins to target Windows Servers using WinRM.

Obviously we could have used a Windows Jenkins server and download one of the Jenkins module named PowerShell… but no, you don’t have powershell.exe on your CentOS 🙂

So we have a Linux server and we want a tools installed on it that’ll use WinRM… let’s have a look to WinRM libraries that exists on the interwebz:

As i already know (a little of) Python basics i decided to go to the PyWinRM client. the installation is pretty easy. If you have a Debian based Linux, you just have to follow the Github Readme file. If, like you have a Redhat based, it’s pretty similar:

# Python installation
yum install Python

Once Python is installed, you want to have pip to be available, there is script for that, available here

# pip installation

Now you just have to follow the from PyWinRM Github project and do this

pip install pywinrm

I used an Azure CentOS VM, so i didn’t need to install kerberos package over pip, but you may need to.

On your Windows target, please configure WinRM like this, remember that it’s not suited for your production env.

winrm set winrm/config/client/auth @{Basic="true"}
winrm set winrm/config/service/auth @{Basic="true"}
winrm set winrm/config/service @{AllowUnencrypted="true"}

Go back to the Jenkins host, and let’s try some of the PyWinRM examples

import winrm
ps_script = """$strComputer = $Host
$RAM = WmiObject Win32_ComputerSystem
$MB = 1048576

"Installed Memory: " + [int]($RAM.TotalPhysicalMemory /$MB) + " MB" """
s = winrm.Session('*.*.*.*', auth=('Administrateur', '<somepassword'))
r = s.run_ps(ps_script)

And let’s execute it with Python !

WinRM and Powershell Python testCool, it seems everything is OK with my WinRM client. I heard you that i’m not using Kerberos here, and you are right, i don’t have Active Directory in this platform to test… Feel free to test and comment 😉

So now, how can i use it with Jenkins ? Answer: Easy !

Go to your Jenkins menu, and choose Manage Jenkins

Manage JenkinsNow in the Manage Jenkins page, click on Manage Plugins

Manage Plugins

Now Click on the Available layer, and in the Filter folder,  search for Python Plugin. Click on Download now and install after restart

Install Jenkins Python Plugin

Now, on your project, go to the end and click on Add build Step, choose Execute Python script

Execute Python script

Now, in the blank window copy/paste the following script in order to test that it’s working (don’t forget to change ip address, login and password 😉

import winrm
s = winrm.Session('*;*;*;*', auth=('Administrateur', '......'))
r = s.run_ps('New-Item -Path D:\\test.txt -Value Hello_World -Type file -Force')

You should have something lke that..

Execute Pythiin script before buildThen click on Save at the bottom of the page.

On the same page, launch a build by click Build Now

Build python winrm powershell script

Now, let’s look at your Window host

Verif Python powershell Jenkins

Everything is ok !

Hope it can help you, feel free to comment if you have any kind of remarks. Obvisouly i’m not an expert with Jenkins so it’s possible that some better tricks can do the job also.