Running Powershell Script not working

595 views
Skip to first unread message

Mark Matthews

unread,
Feb 8, 2016, 10:23:01 AM2/8/16
to Ansible Project
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:

---
- name: Enable Remote Desktop on
  hosts: all
  tasks:
    - name: Enable Remote Desktop
      script: files/Enable_RDP.ps1


Error:
changed: [10.10.3.170] => {"changed": true, "invocation": {"module_args": {"_raw_params": "files/Enable_RDP.ps1"}, "module_name": "script"}, "rc": 0, "stderr": "#< CLIXML\r\n<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\"><S S=\"Error\">set-ItemProperty : Cannot find path _x000D__x000A_</S><S S=\"Error\">'HKLM:\\SystemCurrentControlSetControlTerminal Server' because it does not _x000D__x000A_</S><S S=\"Error\">exist._x000D__x000A_</S><S S=\"Error\">At C:\\Users\\Administrator\\AppData\\Local\\Temp\\ansible-tmp-1454943953.17-24292631_x000D__x000A_</S><S S=\"Error\">07433\\Enable_RDP.ps1:2 char:1_x000D__x000A_</S><S S=\"Error\">+ set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal _x000D__x000A_</S><S S=\"Error\">Server'-name ..._x000D__x000A_</S><S S=\"Error\">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D__x000A_</S><S S=\"Error\">~~~_x000D__x000A_</S><S S=\"Error\"> + CategoryInfo : ObjectNotFound: (HKLM:\\SystemCur...Terminal Serv _x000D__x000A_</S><S S=\"Error\"> er:String) [Set-ItemProperty], ItemNotFoundException_x000D__x000A_</S><S S=\"Error\"> + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetIt _x000D__x000A_</S><S S=\"Error\"> emPropertyCommand_x000D__x000A_</S><S S=\"Error\"> _x000D__x000A_</S></Objs>", "stdout": "", "stdout_lines": []}

J Hawkesworth

unread,
Feb 9, 2016, 5:44:28 AM2/9/16
to Ansible Project
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


Something like (not tested):

win_regedit:
  key: 'HKLM:\System\CurrentControlSet\Control\Terminal Server'
  value: fDenyTSConnections
  data:
    datatype: dword      

Hope this helps,

Jon

Mark Matthews

unread,
Feb 9, 2016, 5:55:13 AM2/9/16
to Ansible Project
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\"><S S=\"Error\">set-ItemProperty : Cannot find path 'C:\\Users\\Administrator\\HKEY_LOCAL_MACHINE\\_x000D__x000A_</S><S S=\"Error\">System\\CurrentControlSet\\Control\\Terminal Server' because it does not exist._x000D__x000A_</S><S S=\"Error\">At line:1 char:1_x000D__x000A_</S><S S=\"Error\">+ set-ItemProperty -Path _x000D__x000A_</S><S S=\"Error\">'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Term ..._x000D__x000A_</S><S S=\"Error\">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D__x000A_</S><S S=\"Error\">~~~_x000D__x000A_</S><S S=\"Error\"> + CategoryInfo : ObjectNotFound: (C:\\Users\\Admini...Terminal Serv _x000D__x000A_</S><S S=\"Error\"> er:String) [Set-ItemProperty], ItemNotFoundException_x000D__x000A_</S><S S=\"Error\"> + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetIt _x000D__x000A_</S><S S=\"Error\"> emPropertyCommand_x000D__x000A_</S><S S=\"Error\"> _x000D__x000A_</S></Objs>", "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_</S>

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

Any ideas would be appreciated.

Cheers
Mark

J Hawkesworth

unread,
Feb 9, 2016, 11:34:09 AM2/9/16
to Ansible Project
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

Mark Matthews

unread,
Feb 9, 2016, 12:30:01 PM2/9/16
to Ansible Project
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!


On Monday, February 8, 2016 at 3:23:01 PM UTC, Mark Matthews wrote:
Reply all
Reply to author
Forward
0 new messages