Running powershell scripts against windows hosts?

I’m looking for some guidance on getting Remote Execution working for my Windows 10 and 2019 hosts so that I can run powershell code on them, ideally via WinRM.

I’m using Foreman in my homelab environment in efforts to familiarize myself with it as well as have better management over my various servers.

I’ve gotten things working pretty well for my Linux servers where I can now manage their packages as well as run scripts against them via Foreman directly, although am having some difficulty figuring out how I can add in Remote Execution, allowing me to run powershell code against my Windows systems in order to do tasks such as run windows update on demand, install and configure software, etc.

I’ve seen some posts on this board which reference ansible and WinRM, although I’ve not found any specific guides that cover how to set it up or the steps required. This is my first time interacting with Ansible so I’m terribly familiar with it.

I’ve already installed the Ansible role on my Foreman server but am not really certain what steps to take next.

Thank you!

So if you want to use Windows with winrm while still using ssh for Linux-machines, you need to overwrite some ansible_settings in your hostgroup (or OS or host itself).

In our windows hostgroups we have following variables set (I am not sure if everything is needed, because I am not an expert on windows):

ansible_become = no
ansible_become_method = enable
ansible_connection = winrm
ansible_port = 5985
ansible_ssh_pass = <password for ansibleuser>
ansible_user = <user for ansible>
ansible_winrm_scheme = http
ansible_winrm_transport = basic

Probably there is room for improvement but it should be a starting point to get the ansible-connection to work - and after that you can run the job-template with Ansible-Cmd which will then execute your input in a powershell.

1 Like

A good starting point is also Setting up a Windows Host — Ansible Documentation

And for Windows specific playbooks Using Ansible and Windows — Ansible Documentation