# Best way to run powershell command?

**URL:** <https://forum.ansible.com/t/best-way-to-run-powershell-command/27534>\
**Category:** Ansible Project\
**Created:** [May 15, 2018, 3:20pm UTC](https://forum.ansible.com/t/best-way-to-run-powershell-command/27534 "2018-05-15T15:20:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ab\_Generette](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Ab\_Generette](https://forum.ansible.com/u/Ab_Generette)\
**Post date:** [May 15, 2018, 3:20pm UTC](https://forum.ansible.com/t/best-way-to-run-powershell-command/27534/1 "2018-05-15T15:20:20Z")

</div>

On my (winrm) target host, I am able to run the following:

> PS C:\Users\agenerette\> c:/Python27/python.exe c:/xx/run/yy/tests/confluence\_api\_test.py

When I try to incorporate that into a playbook, however, I get a great block of mostly encoded error output that I’m not able to make sense of. I’ve attached a couple of screen-shots to give a more complete picture of what I’m seeing, but here’s an excerpt:

> PLAY [873320-wrk04-ia] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> TASK [Do a test run of Sharefiles and Confluence API scripts…] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> Traceback (most recent call last):
> 
> File “/usr/lib/python2.6/site-packages/ansible/plugins/connection/winrm.py”, line 274, in exec\_command
> 
> result = self.\_winrm\_exec(cmd\_parts[0], cmd\_parts[1:], from\_exec=True)
> 
> File “/usr/lib/python2.6/site-packages/ansible/plugins/connection/winrm.py”, line 219, in \_winrm\_exec
> 
> response = Response(self.protocol.get\_command\_output(self.shell\_id, command\_id))
> 
> File “/home/agenerette/.local/lib/python2.6/site-packages/winrm/protocol.py”, line 398, in get\_command\_output
> 
> self.\_raw\_get\_command\_output(shell\_id, command\_id)
> 
> File “/home/agenerette/.local/lib/python2.6/site-packages/winrm/protocol.py”, line 417, in \_raw\_get\_command\_output
> 
> res = self.send\_message(xmltodict.unparse(req))
> 
> File “/home/agenerette/.local/lib/python2.6/site-packages/winrm/protocol.py”, line 244, in send\_message
> 
> fault = root.find(‘soapenv:Body/soapenv:Fault’, xmlns)
> 
> TypeError: find() takes exactly 2 arguments (3 given)
> 
> failed: [873320-wrk04-ia] (item=c:/t3/run/ae/tests/sharefile\_api\_test.py) =\> {“item”: “c:/t3/run/ae/tests/sharefile\_api\_test.py”, “msg”: "failed to exec cmd PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -EncodedCommand

And here’s a copy of the playbook:

> * * *
> 
> - hosts: “{{ targets }}”
> 
> user: “{{ admin }}”
> 
> gather\_facts: yes
> 
> vars:
> 
> python\_exec: c:/Python27/python.exe
> 
> working\_dir: c:/xx/run/yy
> 
> sharefile\_script: c:/xx/run/yy/tests/sharefile\_api\_test.py
> 
> confl\_script: c:/xx/run/yy/tests/confluence\_api\_test.py
> 
> tasks:
> 
> - name: Do a test run of Sharefiles and Confluence API scripts…
> 
> win\_shell: “‘{{ python\_exec }} {{ item }}’”
> 
> # win\_shell: “Get-Item Env:ldap\_user”
> 
> with\_items:
> 
> - “{{ sharefile\_script }}”
> 
> - “{{ confl\_script }}”
> 
> environment:
> 
> LDAP\_USER: “{{ my\_ldap\_user }}”
> 
> LDAP\_REG\_CODE: “null”
> 
> LDAP\_PASS: “{{ my\_ldap\_pass }}”
> 
> GDOC\_PASS: “{{ my\_gdoc\_pass }}”
> 
> SHARE\_USER: “{{ my\_sharefiles\_user }}”
> 
> SHARE\_PASS: “{{ my\_sharefiles\_pass }}”
> 
> WIKI\_USERNAME: “{{ my\_ldap\_user }}”
> 
> WIKI\_PASSWORD: “{{ my\_ldap\_pass }}”
> 
> ignore\_errors: yes
> 
> register: result
> 
> - debug: var=result

[![](https://lh3.googleusercontent.com/-AYwGu7dFiP4/Wvr6XppgvfI/AAAAAAAABP8/DMk7nhc77WgQ0rbKnB6VkarHS3XEiwfogCLcBGAs/s320/Screen%2BShot%2B2018-05-15%2Bat%2B11.16.41%2BAM.png)](https://lh3.googleusercontent.com/-AYwGu7dFiP4/Wvr6XppgvfI/AAAAAAAABP8/DMk7nhc77WgQ0rbKnB6VkarHS3XEiwfogCLcBGAs/s1600/Screen%2BShot%2B2018-05-15%2Bat%2B11.16.41%2BAM.png)

[![](https://lh3.googleusercontent.com/-SemZOrsECIY/Wvr6bf4frNI/AAAAAAAABQA/3MerSpR9958Oml0bHyluR3UGZAdzP9j2gCLcBGAs/s320/Screen%2BShot%2B2018-05-15%2Bat%2B11.17.12%2BAM.png)](https://lh3.googleusercontent.com/-SemZOrsECIY/Wvr6bf4frNI/AAAAAAAABQA/3MerSpR9958Oml0bHyluR3UGZAdzP9j2gCLcBGAs/s1600/Screen%2BShot%2B2018-05-15%2Bat%2B11.17.12%2BAM.png)

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 15, 2018, 7:42pm UTC](https://forum.ansible.com/t/best-way-to-run-powershell-command/27534/2 "2018-05-15T19:42:29Z")

</div>

The main issue you have is that there is a compatibility issue with pywinrm 0.3.0 and Pythoon 2.6 as shown here [https://github.com/diyan/pywinrm/issues/204](https://github.com/diyan/pywinrm/issues/204). This is a controller side issue and you either need to downgrade pywinrm to 0.2.2 or use Python 2.7, 3.5+.

Also what version of Ansible are you running, it doesn’t look like it is pipelining the modules which was added in 2.3?

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Ab\_Generette](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Ab\_Generette](https://forum.ansible.com/u/Ab_Generette)\
**Post date:** [May 15, 2018, 8:27pm UTC](https://forum.ansible.com/t/best-way-to-run-powershell-command/27534/3 "2018-05-15T20:27:02Z")

</div>

Ok, I think I’m going to pull back and work on upgrading both Python (to 2.7.5) and Ansible. We’re 2.2.1.0, where the latter is concerned.

Thanks for your help, Jordan.

-Anthony

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 15, 2018, 9:42pm UTC](https://forum.ansible.com/t/best-way-to-run-powershell-command/27534/4 "2018-05-15T21:42:21Z")

</div>

No worries, I was just curious as to why it wasn’t pipelining. The syntax you had there seemed good to me, the only thing I would recommend is to use win\_command instead of win\_shell when calling the Python executable. The win\_shell module should be used to execute things in a shell interpreter like PowerShell commands while win\_command is used to execute executables.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Ab\_Generette](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Ab\_Generette](https://forum.ansible.com/u/Ab_Generette)\
**Post date:** [May 15, 2018, 10:13pm UTC](https://forum.ansible.com/t/best-way-to-run-powershell-command/27534/5 "2018-05-15T22:13:53Z")

</div>

Ah, ok. I thought I needed to use win\_shell, because of the need to set environment variables. I see that’s not the case, though. Thx.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 15, 2018, 10:30pm UTC](https://forum.ansible.com/t/best-way-to-run-powershell-command/27534/6 "2018-05-15T22:30:47Z")

</div>

Environment variables are set for all modules, except raw (and maybe script I can’t remember). It is a mechanism done by Ansible before running the module code.

Thanks

Jordan
