Python winrm

This article is about run the PowerShell command to the windows node machine via a python script. Execute PowerShell command to windows machine by using Jenkins. Jenkins is originally based on a Linux machine. So, we have to execute the PowerShell command over any windows machine. Here we use python scripts and winrm. WINRM is used to execute Windows commands in a remote server. And python is used to write a script to execute Command.

reference link: https://github.com/diyan/pywinrm/blob/master/README.md


WHY WE USED THIS..?

If you have a Linux-based Jenkins and want to automate the build process on Windows platforms how can we do, without using a Jenkins Window slaves plugin? Let me explain to you what we did to have Jenkins 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

Below explanations is also one of the small ways to execute the command on windows.


REQUIRMENTS

One Jenkins machine with a Linux operations system. And window operating system machines windows10 or windows 12 server download.


JENKINS MACHINE REQUIREMENT

In a Jenkins Linux machine, we have to install a predefined package like python, pywinrm, and Kerberos (network protocol), etc.

(To use Kerberos authentication you need these optional dependencies)


Pywinrm:

pywinrm is a Python client for the Windows Remote Management (WinRM) service. It allows you to invoke commands on target Windows machines from any machine that can run Python.

WinRM allows you to perform various management tasks remotely. These include, but are not limited to: running batch scripts, PowerShell scripts, and fetching WMI variables.


Installation Steps:

To install pywinrm with support for a basic, certificate, and NTLM auth, simply

$ yum install epel-release

$ yum install python-pip python-wheel

$ yum install gcc krb5-devel krb5-workstation

$ pip install Kerberos pykerberos

$ pip install pywinrm

Kerberos authentication you need these optional dependencies.

$ pip install pywinrm[kerberos]


WINDOWS MACHINE REQUIREMENT

On a Windows machine, we have to enable and Configure winrm.

Once Powershell has been upgraded to at least version 3.0, the final step is for the WinRM service to be configured so that Ansible can connect to it. There are two main components of the WinRM service that governs how python can interface with the Windows host: the listener and the service configuration settings.


Go through Upcoming Commands :

To use this script, run the following in PowerShell:

➢$url="https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"

➢ $file = "$env:temp\ConfigureRemotingForAnsible.ps1"

➢ (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)

➢ powershell.exe -ExecutionPolicy ByPass -File $file


WinRM Listener:

The WinRM services listen for requests on one or more ports. Each of these ports must have a listener created and configured.

➢ $ winrm enumerate winrm/config/Listener


Out put of above Command :

Listener

Address = *

Transport = HTTP

Port = 5985

Hostname

Enabled = true

URLPrefix = wsman

CertificateThumbprint

ListeningOn = 10.0.2.15, 127.0.0.1, 192.168.56.155, ::1, fe80::5efe:10.0.2.15%6,

fe80::5efe:192.168.56.155%8, fe80::

ffff:ffff:fffe%2, fe80::203d:7d97:c2ed:ec78%3, fe80::e8ea:d765:2c69:7756%7

Listener

Address = *

Transport = HTTPS

Port = 5986

Hostname = SERVER2016

Enabled = true

URLPrefix = wsman

CertificateThumbprint = E6CDAA82EEAF2ECE8546E05DB7F3E01AA47D76CE

ListeningOn = 10.0.2.15, 127.0.0.1, 192.168.56.155, ::1, fe80::5efe:10.0.2.15%6,

fe80::5efe:192.168.56.155%8, fe80::

ffff:ffff:fffe%2, fe80::203d:7d97:c2ed:ec78%3, fe80::e8ea:d765:2c69:7756%7


Then Config and enable winrm. On your Windows target, please configure WinRM like this,

remember that it’s not suited for your production env.

Goto search > cmd > Run as administrator

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

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

$ winrm set winrm/config/service @{AllowUnencrypted="true"}


And let’s execute it with Python!

Everything ok And let’s execute it with Python!


JENKINS

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

Go to your Jenkins menu, and choose Manage Jenkins

Now on the Manage Jenkins page, click on 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


Now, on your project, go to the end and click on Add build Step, choose 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('windows_IP', auth=('username', 'password'))

r = s.run_cmd('choco install git -y')

r.status_code

r.std_out



Then click on Save at the bottom of the page.

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


Now, let’s look at your Window host

previous windows machine


RESULT

the output of that command:


Recent Comments

No comments

Leave a Comment