# \[Windows\] When executed locally, powershell script works, but over winrm I get errors

**URL:** <https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [January 23, 2015, 8:00pm UTC](https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363 "2015-01-23T20:00:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John-Paul\_Herold](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@John-Paul\_Herold](https://forum.ansible.com/u/John-Paul_Herold)\
**Post date:** [January 23, 2015, 8:00pm UTC](https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363/1 "2015-01-23T20:00:00Z")

</div>

Greetings,

I am on the linux side of my company, but have been working with the windows team to increase their level of automation. I have been having our windows devs script out anything the native ansible windows modules cannot do, and have had a 99% success rate with all our tasks. The one script that doesn’t work is a script that creates virtual directories. Now I normally would think it was a scripting bug (which it may still be), however this is my curious observation:

**When run locally on a windows 2012 box, the script creates the virtual directories, with no errors. When run remotely via Ansible over winrm, the script returns errors.**

This makes me think that there is something going on behind the scenes. I wanted to look into the issue, but from my understanding the script module really just pushes the script to the remote host and executes it, correct? I’m trying to research what other variables may be playing into the equation and wanted to see if anyone had any suggestions?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 23, 2015, 8:02pm UTC](https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363/2 "2015-01-23T20:02:13Z")

</div>

without the actual errors, it is hard to help. There are many  
differences between local execution from the command line and  
executing over winrm, but mostly due to the nature of winrm.

---

<div class="post-metadata">

**Author:** ![John-Paul\_Herold](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@John-Paul\_Herold](https://forum.ansible.com/u/John-Paul_Herold)\
**Post date:** [January 23, 2015, 8:37pm UTC](https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363/3 "2015-01-23T20:37:56Z")

</div>

Hey Brian,

Here is a paste of the errors: [The easiest way to host your text](http://tny.cz/10fb3baa)

Seems like the issue is with how the params are passed to the script over winrm. This leads me to some ambiguity I’ve had with proper formatting when it comes to Ansible’s script module script params (that’s a confusing description ha). There are a couple cases where I’ve had to do things like the following for proper execution (the three examples below, from other tasks, work):

# Had to use nested quotes to prevent the colon from path from tampering with how the param was read

`

- name: ‘[29/45] Create Reports\_ui web application’  
script: ‘CreateApplication.ps1 Reports\_ui “C:\web\path\Reports\_ui” Reports\_ui Company’  
`

# Example of params that did not need any type of syntax adjustment

`

- name: ‘[28/45] Create Reports app pool’  
script: CreateAppPool.ps1 Reports\_ui True Classic v4.0  
`

# Example of escaping backslash

`

- name: ‘[12/45] Create Company\chp folder’  
script: CreateFolder.ps1 Company\chp  
`

Most of those were settled on after multiple trials, until a successful execution occurred. This leads me to the script that I opened this discussion about, and the original way in which I call it:

`

- name: ‘[37/45] Create AutoImportRoot virtual directory’  
script: CreateVirtualDirectory.ps1 Company ui AutoImportRoot

`

So it doesn’t have anything too fancy in terms of the params like some of the above examples have. But still, the script isn’t running the same over winrm, and exceptions look parameter related. I saw on another thread that setting ANSIBLE\_KEEP\_REMOTE\_FILES=1 would allow me to actually see the script executed on the host?

---

<div class="post-metadata">

**Author:** ![John-Paul\_Herold](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@John-Paul\_Herold](https://forum.ansible.com/u/John-Paul_Herold)\
**Post date:** [January 24, 2015, 6:22pm UTC](https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363/4 "2015-01-24T18:22:35Z")

</div>

My case is rather specific, understandably. But if anyone has had issues with powershell scripts running over winrm and successfully debugged them, any tips/references (either Ansible or winrm related) would be greatly appreciated. I’m reading through technet articles and other resources learning how to troubleshoot winrm and just about the service as a whole.

Thanks,

---

<div class="post-metadata">

**Author:** ![Jonathan\_Davila](https://avatars.discourse-cdn.com/v4/letter/j/e0b2c6/32.png) [@Jonathan\_Davila](https://forum.ansible.com/u/Jonathan_Davila)\
**Post date:** [January 25, 2015, 3:09pm UTC](https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363/5 "2015-01-25T15:09:29Z")

</div>

Personally, I’ve had greater success converting a normal PS script into a proper module. It really isn’t too difficult to do and is probably a better way to go in the long term anyways. Have you tried doing that? Or just exclusively executing via the script module?

---

<div class="post-metadata">

**Author:** ![John-Paul\_Herold](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@John-Paul\_Herold](https://forum.ansible.com/u/John-Paul_Herold)\
**Post date:** [January 25, 2015, 10:30pm UTC](https://forum.ansible.com/t/windows-when-executed-locally-powershell-script-works-but-over-winrm-i-get-errors/17363/6 "2015-01-25T22:30:01Z")

</div>

Hey Jonathan, appreciate the advice, I think I may start doing just that. Will keep you guys updated.

I only have executed the script up to this point, and I am almost more intrigued in why the differences between local execution and remote (via winrm) execution. Have a feeling I should ease my stubborn engineering mind and throw this in the bucket of strange Windows behaviors.
