2016-02-09 33 views
1

我试图运行一个简单的手册来运行Powershell脚本。但它一直在失败。如果我直接在服务器上运行Powershell脚本,它工作正常。以下是剧本和脚本。 有什么需要做Ansible来运行Powershell脚本?Ansible中的Powershell脚本

PowerShell脚本(rdp.ps1):

set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server'-name "fDenyTSConnections" -Value 0 

剧本:

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

错误:

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": []}

所以我编辑的剧本尝试运行使用命令“原始的'命令......但这些也失败了。

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

我也试过没有',像这样:

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

我然后使用原始命令创建一个文件夹,运行以下剧本。它的工作。一个非常基本的Powershell命令。

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

什么可能导致一个剧本失败,另一个成功?当然Ansible应该能够运行任何Powershell脚本?或者是否有某种先决条件或特定方式来编写这些Powershell脚本以使其工作?

  • Ansible是否能够运行和执行Powershell脚本?
  • 任何类型的Powershell脚本?
  • Powershell脚本是否需要以特定方式编写脚本以供Ansible执行?
+0

我认为你正在寻找的HKLM路径是' HKLM:\ System \ CurrentControlSet \ Control \ Terminal Server(注意斜杠) –

+0

这就是问题所在!谢谢 – Mark

回答

4

PowerShell是告诉Ansible它“无法找到路径”来:

HKLM:SystemCurrentControlSetControlTerminal Server 

原因很简单,不存在这样的路径! :-)

看来,在该字符串的路径分隔符相处的方式输了,这就是你正在寻找的路径:

HKLM:\System\CurrentControlSet\Control\Terminal Server