Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

Execute PowerShell Commands on Windows via Python Script

Execute PowerShell Commands on Windows via Python Script

If you’re working with Jenkins on a Linux machine and need to automate tasks on a Windows server, running PowerShell commands remotely is a crucial skill. In this article, we will explore how to execute PowerShell commands on a Windows machine through Python scripts, leveraging Jenkins for seamless integration. We’ll also discuss the role of WinRM (Windows Remote Management) in facilitating remote command execution and show you how to set everything up.

Jenkins and Python script executing PowerShell commands on a Windows machine via WinRM

Introduction to WinRM for Remote Command Execution

WinRM is a Microsoft service that allows remote management of Windows machines. It’s commonly used to execute commands and scripts on Windows servers. If you’re using a Linux-based Jenkins server, you can still interact with Windows servers by setting up WinRM. By using Python scripts in combination with WinRM, you can automate tasks on your Windows servers, such as executing PowerShell commands or running batch scripts.

To automate this process without requiring a Windows Jenkins server, follow these steps to configure Jenkins, WinRM, and Python.

Why Execute PowerShell Commands Remotely?

Sometimes, a Jenkins setup on a Linux machine may need to target Windows servers for automation tasks. While there are plugins that can enable direct communication with Windows (like PowerShell plugins), using a more flexible approach through Python and WinRM can offer significant advantages, especially if you’re managing a multi-platform environment.

For example, with Python scripts, you can write automation processes that interact with your infrastructure’s Windows nodes, whether they’re Windows 10, 11, or Windows Server versions. ZippyOPS, offering DevOps, DevSecOps, DataOps, and cloud infrastructure services, can assist in building efficient, secure, and scalable automation pipelines across multi-cloud environments.

Setting Up Jenkins and Python for Remote Execution

To get started with executing PowerShell commands remotely, there are a few setup steps you’ll need to follow for both Jenkins and Windows machines.

1. Jenkins Machine Requirements

Your Jenkins server, running on Linux, needs several tools and packages installed to communicate with Windows hosts.

  • Python: Ensure Python is installed on your Jenkins machine.
  • PyWinRM: A Python client for Windows Remote Management (WinRM).
  • Kerberos (optional): For secure authentication over WinRM.

Installing Required Packages

To install the necessary packages, use the following commands:

$ yum install epel-release
$ yum install python-pip python-wheel
$ yum install gcc krb5-devel krb5-workstation
$ pip install Kerberos pykerberos
$ pip install pywinrm

To enable Kerberos authentication, install pywinrm with the following command:

$ pip install pywinrm[kerberos]

2. Configuring the Windows Machine for WinRM

On the Windows machine, you need to enable WinRM, which allows Jenkins (or any Python script) to interact with it remotely. After upgrading PowerShell to version 3.0 or later, configure WinRM by running the following commands 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

Next, you need to configure WinRM listeners. These listeners determine on which ports WinRM will listen for incoming requests.

$ winrm enumerate winrm/config/Listener

After running the above command, you should see output indicating that the listener is set up. Make sure that both HTTP (port 5985) and HTTPS (port 5986) are enabled.

Configuring Authentication and Encryption

To enable basic authentication and allow unencrypted connections for testing purposes (not recommended for production), run:

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

3. Running PowerShell Commands Remotely with Python

Once both the Jenkins and Windows machines are configured, you can now use a Python script to run PowerShell commands on the Windows server. Here’s an example Python script that executes a command remotely using the winrm Python library:

import winrm

s = winrm.Session('windows_IP', auth=('username', 'password'))
r = s.run_cmd('choco install git -y')

print(r.status_code)
print(r.std_out)

Make sure to replace 'windows_IP', 'username', and 'password' with the actual IP address and credentials for your Windows machine.

Automating the Process with Jenkins

With your Jenkins server set up and ready, you can easily automate the process of running PowerShell commands remotely from within Jenkins itself. To do this:

  1. Navigate to Manage Jenkins.
  2. Click on Manage Plugins and install the Python Plugin.
  3. On your Jenkins project page, add a Build Step and select Execute Python script.
  4. Paste the Python script into the window and save.

Once you save your Jenkins job, you can trigger the build by clicking Build Now. Jenkins will execute the Python script, which in turn communicates with the Windows machine via WinRM to run the specified PowerShell command.

Conclusion: Streamlining Automation with ZippyOPS

Executing PowerShell commands on Windows remotely using Python and Jenkins is a powerful way to automate tasks in a multi-platform environment. By integrating tools like WinRM and Python scripts into your Jenkins pipeline, you can efficiently manage Windows servers from a Linux-based Jenkins setup.

If you’re looking to build a robust automation pipeline or need help managing complex multi-cloud environments, ZippyOPS offers expert consulting, implementation, and managed services in DevOps, DevSecOps, Cloud, and Automation. We specialize in optimizing infrastructure and security with services such as AIOps, MLOps, Microservices, and more.

For a seamless transition to a fully automated, scalable infrastructure, contact us at sales@zippyops.com today!

For more on how WinRM helps with automation tasks in a multi-platform environment, check out Microsoft’s official documentation on WinRM.

Leave a Comment

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

Scroll to Top