Mark
Mark

Reputation: 15

Powershell Scripts in Ansible

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 playbook and the script. Is there something that needs to be done to get Ansible to run the Powershell script?

Powershell Script (rdp.ps1):

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\nhttp://schemas.microsoft.com/powershell/2004/04\(http://schemas.microsoft.com/powershell/2004/04%5C) ">set-ItemProperty : Cannot find path _x000D__x000A_'HKLM:\SystemCurrentControlSetControlTerminal Server' because it does not _x000D__x000A_exist._x000D__x000A_At C:\Users\Administrator\AppData\Local\Temp\ansible-tmp-1454943953.17-24292631_x000D__x000A_07433\Enable_RDP.ps1:2 char:1_x000D__x000A_+ set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal _x000D__x000A_Server'-name ..._x000D__x000A_+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D__x000A_~~~_x000D__x000A_ + CategoryInfo : ObjectNotFound: (HKLM:\SystemCur...Terminal Serv _x000D__x000A_ er:String) [Set-ItemProperty], ItemNotFoundException_x000D__x000A_ + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetIt _x000D__x000A_ emPropertyCommand_x000D__x000A_ _x000D__x000A_", "stdout": "", "stdout_lines": []}

So I edited the playbook to try run the command using the ‘raw’ command…..but these also failed.

--- 
- name: Enable Remote Desktop on 
  hosts: all 
  tasks: 
    - name: Enable Remote Desktop 
      raw: 'set-ItemProperty -Path "HKLM:SystemCurrentControlSetControlTerminal Server"-name "fDenyTSConnections" -Value 0'

I also tried without the ' ', like this:

--- 
- name: Enable Remote Desktop on 
  hosts: all 
  tasks: 
    - name: Enable Remote Desktop 
      raw: set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server'-name "fDenyTSConnections" -Value 0

I then ran the following playbook using the Raw command to create a folder. And it worked. A very basic Powershell command.

--- 
- name: Enable Remote Desktop on 
  hosts: all 
  tasks: 
    - name: Enable Remote Desktop 
      raw: New-Item -Path c:\test3 -ItemType directory

What could be causing the one playbook to fail and the other to succeed? Surely Ansible should be able to run any Powershell script? Or is there some kind of pre-requisite or specific way of writing these Powershell scripts in order for them to work?

Upvotes: 1

Views: 6719

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

PowerShell is telling Ansible that it "Cannot find the path" to:

HKLM:SystemCurrentControlSetControlTerminal Server

For the simple reason that no such path exists! :-)

It seems that the path separator in that string got lost along the way, this is the path you're looking for:

HKLM:\System\CurrentControlSet\Control\Terminal Server

Upvotes: 4

Related Questions