Getting a batch to run as administrator Windows

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

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

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.