# Getting a batch to run as administrator Windows

**URL:** <https://forum.ansible.com/t/getting-a-batch-to-run-as-administrator-windows/26559>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [January 24, 2018, 5:30pm UTC](https://forum.ansible.com/t/getting-a-batch-to-run-as-administrator-windows/26559 "2018-01-24T17:30:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpescatore](https://avatars.discourse-cdn.com/v4/letter/l/ccd318/32.png) [@lpescatore](https://forum.ansible.com/u/lpescatore)\
**Post date:** [January 24, 2018, 5:30pm UTC](https://forum.ansible.com/t/getting-a-batch-to-run-as-administrator-windows/26559/1 "2018-01-24T17:30:39Z")

</div>

Hi,

TL;DR - need the “run as administrator” to be automated.

I have tried and tried to get a bat file to run as administrator on a windows box.  
What it is supposed to do is chmod a file in cygwin.  
Manually, if i right click and run as administrator, it works fine.  
If I run my play (below), it gives no error, but does not do anything (i run the bat manually, not as admin, and get an error saying permission denied, so it’s def a permissions issue that running as admin solves).  
I tried win\_ps\_exec, but I get an error there saying psexec.exe not found.  
What can I do to fix?

- name: copy chmod batch file over

win\_copy:

src: /ansible/batch/chmod.bat

dest: C:\TEMP\chmod.bat

- name: copy chmod sh file over

win\_copy:

src: /ansible/batch/chmod.sh

dest: C:\TEMP\chmod.sh

- name: Run Batch File

win\_command: C:\TEMP\chmod.bat

Contents of bat:

C:\cygwin\bin\mintty.exe C:\TEMP\chmod.sh

Contents of sh:

**chmod** 660 /etc/rsyncd.secrets

---

<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:** [January 24, 2018, 9:21pm UTC](https://forum.ansible.com/t/getting-a-batch-to-run-as-administrator-windows/26559/2 "2018-01-24T21:21:29Z")

</div>

I don’t think it is an elevation issue but rather a WinRM issue. All commands run in WinRM run under an elevated token, you can test this out by running

`  
ansible -i inventory.ini hosts -m win\_command -a “whoami /all”

`

You would get a similar output to the below

`  
(ansible-py36) jborean:~/dev/module-tester$ ansible -i inventory.ini ‘2016’ -m win\_command -a “whoami /all”  
SERVER2016.domain.local | SUCCESS | rc=0 \>\>

USER INFORMATION

---

<div class="post-metadata">

**Author:** ![lpescatore](https://avatars.discourse-cdn.com/v4/letter/l/ccd318/32.png) [@lpescatore](https://forum.ansible.com/u/lpescatore)\
**Post date:** [January 25, 2018, 5:12pm UTC](https://forum.ansible.com/t/getting-a-batch-to-run-as-administrator-windows/26559/3 "2018-01-25T17:12:34Z")

</div>

Looks like the become works!  
However, the syntax needs to be in this case:

win\_command: ‘cmd.exe /c “start C:\cygwin\bin\bash -li /cygdrive/c/TEMP/chmod.sh”’

become: yes

become\_method: runas

become\_user: SYSTEM

I appreciate your help! I have like 4 or 5 scripts that I need elevation to run.
