title: Powershell Scripts in Ansible
body: <p>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?</p>
  
  <p>Powershell Script (rdp.ps1):</p>
  
  <pre><code>set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server'-name "fDenyTSConnections" -Value 0
  </code></pre>
  
  <p>Playbook:</p>
  
  <pre><code>---
  - name: Enable Remote Desktop on
    hosts: all
    tasks:
      - name: Enable Remote Desktop
        script: files/Enable_RDP.ps1
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>changed: [10.10.3.170] => {"changed": true, "invocation": {"module_args": {"_raw_params": "files/Enable_RDP.ps1"}, "module_name": "script"}, "rc": 0, "stderr": "#&lt; 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": []}</p>
  </blockquote>
  
  <p>So I edited the playbook to try run the command using the ‘raw’ command…..but these also failed.</p>
  
  <pre><code>--- 
  - name: Enable Remote Desktop on 
    hosts: all 
    tasks: 
      - name: Enable Remote Desktop 
        raw: 'set-ItemProperty -Path "HKLM:SystemCurrentControlSetControlTerminal Server"-name "fDenyTSConnections" -Value 0'
  </code></pre>
  
  <p>I also tried without the ' ', like this: </p>
  
  <pre><code>--- 
  - name: Enable Remote Desktop on 
    hosts: all 
    tasks: 
      - name: Enable Remote Desktop 
        raw: set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server'-name "fDenyTSConnections" -Value 0
  </code></pre>
  
  <p>I then ran the following playbook using the Raw command to create a folder. And it worked. A very basic Powershell command. </p>
  
  <pre><code>--- 
  - name: Enable Remote Desktop on 
    hosts: all 
    tasks: 
      - name: Enable Remote Desktop 
        raw: New-Item -Path c:\test3 -ItemType directory
  </code></pre>
  
  <p>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? </p>
  
  <ul>
  <li>Is Ansible able to run and perform Powershell scripts? </li>
  <li>Any kind of Powershell script?</li>
  <li>Does the Powershell script need to be scripted in a particular way for Ansible to perform it?</li>
  </ul>