# Running Powershell Script not working

**URL:** <https://forum.ansible.com/t/running-powershell-script-not-working/20487>\
**Category:** Ansible Project\
**Created:** [February 8, 2016, 3:23pm UTC](https://forum.ansible.com/t/running-powershell-script-not-working/20487 "2016-02-08T15:23:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [February 8, 2016, 3:23pm UTC](https://forum.ansible.com/t/running-powershell-script-not-working/20487/1 "2016-02-08T15:23:01Z")

</div>

Hi

I am trying to run a simple playbook to run a Powershell script. But it keeps failing.  
If I run the Powershell script directly on the server it works fine. Below is the play book ans the script.

Is there something that needs to be done to get Ansible to run the Powershell script?

Powershell Scripts (rdp.ps1)

# Enable Remote Desktop

set-ItemProperty -Path ‘HKLM:SystemCurrentControlSetControlTerminal Server’-name “fDenyTSConnections” -Value 0

Playbook:

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [February 9, 2016, 10:44am UTC](https://forum.ansible.com/t/running-powershell-script-not-working/20487/2 "2016-02-09T10:44:28Z")

</div>

Looks like you have lost some backslashes somehow

instead of

set-ItemProperty -Path ‘HKLM:SystemCurrentControlSetControlTerminal Server’-name “fDenyTSConnections” -Value 0

try

set-ItemProperty -Path ‘HKLM:System\CurrentControlSet\Control\Terminal Server’-name “fDenyTSConnections” -Value 0

Or, if you are running ansible 2.0, try using the win\_regedit module

[http://docs.ansible.com/ansible/win\_regedit\_module.html](http://docs.ansible.com/ansible/win_regedit_module.html)

Something like (not tested):

```auto
win_regedit:
  key: 'HKLM:\System\CurrentControlSet\Control\Terminal Server'
  value: fDenyTSConnections
  data: 0 

```

datatype: dword

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [February 9, 2016, 10:55am UTC](https://forum.ansible.com/t/running-powershell-script-not-working/20487/3 "2016-02-09T10:55:12Z")

</div>

Thanks for the response!

I have tried the following Powershell options:  
set-ItemProperty -Path ‘HKLM:System\CurrentControlSet\Control\Terminal Server’-name “fDenyTSConnections” -Value 0  
set-ItemProperty -Path ‘HKEY\_LOCAL\_MACHINE:System\CurrentControlSet\Control\Terminal Server’ -name “fDenyTSConnections” -Value 0

set-ItemProperty -Path ‘HKEY\_LOCAL\_MACHINE\System\CurrentControlSet\Control\Terminal Server’ -name “fDenyTSConnections” -Value 0

But none of them work. Got the following error with the option you suggested.

fatal: [10.10.3.170]: FAILED! =\> {“changed”: false, “failed”: true, “invocation”: {“module\_args”: {“_raw\_params": “set-ItemProperty -Path ‘HKEY\_LOCAL\_MACHINE\System\CurrentControlSet\Control\Terminal Server’ -name "fDenyTSConnections" -Value 0”}, “module\_name”: “raw”}, “rc”: 1, “stderr”: “#\< CLIXML\r\n\<Objs Version="1.1.0.1" xmlns="[http://schemas.microsoft.com/powershell/2004/04\](http://schemas.microsoft.com/powershell/2004/04%5C)”\>\<S S="Error"\>set-ItemProperty : Cannot find path ‘C:\Users\Administrator\HKEY\_LOCAL\_MACHINE\x000D\_\_x000A\<S S="Error"\>System\CurrentControlSet\Control\Terminal Server’ because it does not exist.x000D\_\_x000A\<S S="Error"\>At line:1 char:1\_x000D\_\_x000A_\<S S="Error"\>+ set-ItemProperty -Path _x000D\_\_x000A_\<S S="Error"\>'HKEY\_LOCAL\_MACHINE\System\CurrentControlSet\Control\Term …_x000D\_\_x000A_\<S S="Error"\>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D\_\_x000A_~~ \<S S="Error"\>~_x000D\_\_x000A_\<S S="Error"\> + CategoryInfo : ObjectNotFound: (C:\Users\Admini…Terminal Serv _x000D\_\_x000A_\<S S="Error"\> er:String) [Set-ItemProperty], ItemNotFoundException\_x000D\_\_x000A\_\<S S="Error"\> + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetIt _x000D\_\_x000A_\<S S="Error"\> emPropertyCommand\_x000D\_\_x000A\_\<S S="Error"\> _x000D\_\_x000A_”, “stdout”: “”, “stdout\_lines”: }  
I have no doubt if I used the win\_regedit it would work. But Im trying to figure out why Ansible is not able to run a simple Powershell script from a playbook?  
Why am I able to run that script successfully from the machine, but it fails when Ansible ties to execute it from a playbook.

This is the part of the error that concerns me:  
\<S S="Error"\>set-ItemProperty : Cannot find path 'C:\Users\Administrator\HKEY\_LOCAL\_MACHINE\_x000D\_\_x000A_

Why is Ansible trying to find the path in C:\ when this is a registry script?

Any ideas would be appreciated.

Cheers  
Mark

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [February 9, 2016, 4:34pm UTC](https://forum.ansible.com/t/running-powershell-script-not-working/20487/4 "2016-02-09T16:34:08Z")

</div>

My apologies, I missed the leading slash after the ‘drive name’ ( For some reason powershell has its own notion of ‘drives’ which include parts of the registry - HKLM: is the name of the powershell drive that maps to the HKEY\_LOCAL\_MACHINE part ot the registry.)

Set-ItemProperty -Path ‘HKLM:\System\CurrentControlSet\Control\Terminal Server’ -name “fDenyTSConnections” -Value 0

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Mark\_Matthews](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mark\_Matthews](https://forum.ansible.com/u/Mark_Matthews)\
**Post date:** [February 9, 2016, 5:30pm UTC](https://forum.ansible.com/t/running-powershell-script-not-working/20487/5 "2016-02-09T17:30:00Z")

</div>

You are absolutely right mate!! I literally had tried every way except that!

Once I made that change to the registry entries in the Powershell scripts all the playbooks starting to run successfully.

Thanks for your help!
