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

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?

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.

Hey Brian,

Here is a paste of the errors: 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?

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,

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?

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.