我試圖運行一個簡單的手冊來運行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執行?
我認爲你正在尋找的HKLM路徑是' HKLM:\ System \ CurrentControlSet \ Control \ Terminal Server(注意斜槓) –
這就是問題所在!謝謝 – Mark