Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.

How to Setup Linux to Query Windows WinRM Hosts

By - Source: Toms IT Pro

To run commands on Windows computers from a Linux host requires a few more steps than just enabling it.

PowerShell remoting now provides the ability to remotely execute commands on computers just like being on the remote machine's console. It provides seamless integration with Active Directory and is simple to setup but only if in a Windows environment. Because the technology and tooling around it rely so much on Windows, the process of setting up this communication on a Linux machine is not as straightforward.

MORE: A Brief History of Linux

To run commands on Windows computers from a Linux host requires a few more steps than just enabling it. The first and most obvious step is to ensure PS remoting is enabled on the Windows node. A quick way to do this is to run Enable-PSRemoting.

Enable-PSRemoting -Force

This enables PS remoting, sets up the required WinRM listener and ensures the Windows firewall rule is enabled. Once this is done, you'll need to tweak some things for a Linux host to communicate with Windows. It's easiest to set up this relationship when Active Directory/Kerberos is not involved. This basic authentication requires that we relax security on the Windows node by allowing unencrypted WinRM traffic and enabling basic authentication.

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

Since, by itself, Linux cannot directly speak to a Windows node over WinRM. It will need some help. Luckily, the majority of Linux distributions come with Python installed. A great library called pywinrm exists for Python allowing Python to communicate over WinRM to a Windows host. To get the library, download and install it using pip, the Python, package manager.  First, ensure pip is installed.

sudo easy_install pip

Then, pip can download and install pywinrm.

sudo pip install pywinrm

It's now time to connect to the Windows host. To test this out, we'll first bring up python in interactive mode.


Once in interactive mode, we'll import the WinRM library. If no errors are seen here, this will confirm that the module was installed successfully.

import winrm

Establish a WinRM session to the Windows node. To do that, I'll use the Session method and provide the name or IP address of the node along with the username and password of the local Windows administrator account.

session = winrm.Session(', auth=('username','password'))

If no errors are thrown yet, this means that a session has been established. At this point, a command can be run on the remote Windows node. I'm assuming that PowerShell commands will run on the remote computer, I'll use the run_ps method as an example. This is a method on the session object that allows you to run any command you'd like inside of a PowerShell session on the remote Windows host. When the run_ps() method is executed, it will execute the command on the remote Windows computer and return the output (if any) to stdout on the Linux client. Below you can see the Python syntax to use to capture the output from a command and print it to the console

result = session.run_ps("hostname")

You should now have all the required skills necessary to remotely invoke commands from Linux clients to a Windows host. Depending on the Linux distribution, this process may vary slightly, but since we've used Python, the differences should be minimal.