# How to do stuff on the remote Windows node from the command node?

**URL:** <https://forum.ansible.com/t/how-to-do-stuff-on-the-remote-windows-node-from-the-command-node/26628>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [February 1, 2018, 9:08pm UTC](https://forum.ansible.com/t/how-to-do-stuff-on-the-remote-windows-node-from-the-command-node/26628 "2018-02-01T21:08:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Anderson](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jonathan\_Anderson](https://forum.ansible.com/u/Jonathan_Anderson)\
**Post date:** [February 1, 2018, 9:08pm UTC](https://forum.ansible.com/t/how-to-do-stuff-on-the-remote-windows-node-from-the-command-node/26628/1 "2018-02-01T21:08:45Z")

</div>

Linux Command Node: CentOs 6, Ansible 2.1, Python 2.7.13

Windows Remote Nodes: Windows 7, Powershell 3.0

Encryption: SSL

So far I’ve figured out how to use these tricks in my playbook to do stuff on the remote Windows node from the command node. Are there other methods I’ve left out?

Use [Ansible Modules](http://docs.ansible.com/ansible/latest/list_of_all_modules.html) (especially those [starting with “win\_”](http://docs.ansible.com/ansible/latest/list_of_windows_modules.html)) to do something on a remote node

- name: Copy a windows file from control node to remote node  
[win\_copy](http://docs.ansible.com/ansible/latest/win_copy_module.html): src=“/etc/ansible/winFiles/getFileVersion.exe” dest=“C:/Temp/”

-name: Restart a remote node  
[win\_reboot:](http://docs.ansible.com/ansible/latest/win_reboot_module.html)

Use the Ansible [“raw:” module](http://docs.ansible.com/ansible/latest/raw_module.html) to run commands or executables on the remote node

- name: Issue a Windows command to run on a remote node from control node  
raw: [Rename](https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/rename.mspx?mfr=true)C:/temp/oldName.txt C:/temp/newName.txt

- name: Issue a Powershell Cmdlet to run on a remote node from control node  
raw: [Copy-Item](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-3.0) “C:/source/_._” -Destination “C:/destination/”

- name: Execute windows executable previously copied to a remote node from the control node  
raw: C:/temp/getFileVersion.exe “C:/Windows/driver.dll”

Use the Ansible [“script:”](http://docs.ansible.com/ansible/latest/script_module.html) module to **copy, run and delete** a Powershell module on the remote node from the control node.

- name: run a powershell script on a remote node from the control node  
[script:](http://docs.ansible.com/ansible/latest/script_module.html) /etc/ansible/psScripts/doesItemExist.ps1 “C:/TempBackup/”

Other ideas?

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [February 2, 2018, 4:37am UTC](https://forum.ansible.com/t/how-to-do-stuff-on-the-remote-windows-node-from-the-command-node/26628/2 "2018-02-02T04:37:49Z")

</div>

Here are some further tips for people reading

- Use the yaml syntax when writting task rather than the key=value format, this makes it a lot simpler to write Windows paths.

e.g for the first task do.

`

- name: Copy a windows file from control node to remote node  
win\_copy:  
src: /etc/ansible/winFiles/getFileVersion.exe  
dest: C:\Temp\

`

- Use backslashes for the Windows paths, if you use the yaml syntax, you only need to escape them when using double quote “”

e.g all of the below lead to the same value.

`

- some\_module:  
path: C:\temp  
quoted\_path: ‘C:\temp’  
double\_quoted\_path: “C:\temp”  
`

- Avoid using the raw module, use win\_command or win\_shell instead, you can’t use become or environment variables with raw

- Use win\_command when you are running an executable, avoid using this when running shell commands like dir, del or PowerShell cmdlets like New-Item, Copy-Item and so on

- Use win\_shell when you are running shell commands or powershell cmdlets, avoid using this to run executables as you need to deal with shell escaping for complex arguments

- For tasks that give you an access is denied, use become to run it like it would locally (not experimental as of the 2.5 release)

`

# this does not work and leads to an access is denied error

- name: upgrade powershell  
win\_chocoletey:  
name: poweshell  
state: latest

# when using become this is no longer an issue

- name: upgrade powershell  
win\_chocolatey:  
name: powershell  
state: latest  
become: yes  
become\_method: runas  
become\_user: SYSTEM  
`

- If there is not an Windows module to complete a task you are looking for, look to see if there is an DSC resource instead (PowerShell v5 or newer is required)

e.g. to manage Hyper-V you can use the DSC resource like so

`

- name: install Hyper-V DSC resource  
win\_psmodule:  
name: xHyper-V  
state: present

- name: create Hyper-V VM  
win\_dsc:  
resource\_name: xVMHyperv # the DSC module to use

# the below parameters are all the DSC parameters to set

Ensure: present  
Name: HyperVHost  
VhdPath: C:\temp\vm.vhd  
StartupMemory: 6291456000  
Generation: 2  
ProcessorCount: 2  
`

There are numerous other things that are good to know but you are probably best looking at the new documentation for Windows here [http://docs.ansible.com/ansible/devel/windows.html](http://docs.ansible.com/ansible/devel/windows.html).

Thanks

Jordan
