| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: 2017, Dag Wieers (@dagwieers) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r''' |
| 8 |
--- |
| 9 |
module: win_psexec |
| 10 |
short_description: Runs commands (remotely) as another (privileged) user |
| 11 |
description: |
| 12 |
- Run commands (remotely) through the PsExec service. |
| 13 |
- Run commands as another (domain) user (with elevated privileges). |
| 14 |
requirements: |
| 15 |
- Microsoft PsExec |
| 16 |
options: |
| 17 |
command: |
| 18 |
description: |
| 19 |
- The command line to run through PsExec (limited to 260 characters). |
| 20 |
type: str |
| 21 |
required: yes |
| 22 |
executable: |
| 23 |
description: |
| 24 |
- The location of the PsExec utility (in case it is not located in your PATH). |
| 25 |
type: path |
| 26 |
default: psexec.exe |
| 27 |
hostnames: |
| 28 |
description: |
| 29 |
- The hostnames to run the command. |
| 30 |
- If not provided, the command is run locally. |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
username: |
| 34 |
description: |
| 35 |
- The (remote) user to run the command as. |
| 36 |
- If not provided, the current user is used. |
| 37 |
type: str |
| 38 |
password: |
| 39 |
description: |
| 40 |
- The password for the (remote) user to run the command as. |
| 41 |
- This is mandatory in order authenticate yourself. |
| 42 |
type: str |
| 43 |
chdir: |
| 44 |
description: |
| 45 |
- Run the command from this (remote) directory. |
| 46 |
type: path |
| 47 |
nobanner: |
| 48 |
description: |
| 49 |
- Do not display the startup banner and copyright message. |
| 50 |
- This only works for specific versions of the PsExec binary. |
| 51 |
type: bool |
| 52 |
default: no |
| 53 |
noprofile: |
| 54 |
description: |
| 55 |
- Run the command without loading the account's profile. |
| 56 |
type: bool |
| 57 |
default: no |
| 58 |
elevated: |
| 59 |
description: |
| 60 |
- Run the command with elevated privileges. |
| 61 |
type: bool |
| 62 |
default: no |
| 63 |
interactive: |
| 64 |
description: |
| 65 |
- Run the program so that it interacts with the desktop on the remote system. |
| 66 |
type: bool |
| 67 |
default: no |
| 68 |
session: |
| 69 |
description: |
| 70 |
- Specifies the session ID to use. |
| 71 |
- This parameter works in conjunction with I(interactive). |
| 72 |
- It has no effect when I(interactive) is set to C(no). |
| 73 |
type: int |
| 74 |
limited: |
| 75 |
description: |
| 76 |
- Run the command as limited user (strips the Administrators group and allows only privileges assigned to the Users group). |
| 77 |
type: bool |
| 78 |
default: no |
| 79 |
system: |
| 80 |
description: |
| 81 |
- Run the remote command in the System account. |
| 82 |
type: bool |
| 83 |
default: no |
| 84 |
priority: |
| 85 |
description: |
| 86 |
- Used to run the command at a different priority. |
| 87 |
choices: [ abovenormal, background, belownormal, high, low, realtime ] |
| 88 |
type: str |
| 89 |
timeout: |
| 90 |
description: |
| 91 |
- The connection timeout in seconds |
| 92 |
type: int |
| 93 |
wait: |
| 94 |
description: |
| 95 |
- Wait for the application to terminate. |
| 96 |
- Only use for non-interactive applications. |
| 97 |
type: bool |
| 98 |
default: yes |
| 99 |
notes: |
| 100 |
- More information related to Microsoft PsExec is available from |
| 101 |
U(https://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) |
| 102 |
seealso: |
| 103 |
- module: community.windows.psexec |
| 104 |
- module: ansible.builtin.raw |
| 105 |
- module: ansible.windows.win_command |
| 106 |
- module: ansible.windows.win_shell |
| 107 |
author: |
| 108 |
- Dag Wieers (@dagwieers) |
| 109 |
''' |
| 110 |
|
| 111 |
EXAMPLES = r''' |
| 112 |
- name: Test the PsExec connection to the local system (target node) with your user |
| 113 |
community.windows.win_psexec: |
| 114 |
command: whoami.exe |
| 115 |
|
| 116 |
- name: Run regedit.exe locally (on target node) as SYSTEM and interactively |
| 117 |
community.windows.win_psexec: |
| 118 |
command: regedit.exe |
| 119 |
interactive: true |
| 120 |
system: true |
| 121 |
|
| 122 |
- name: Run the setup.exe installer on multiple servers using the Domain Administrator |
| 123 |
community.windows.win_psexec: |
| 124 |
command: E:\setup.exe /i /IACCEPTEULA |
| 125 |
hostnames: |
| 126 |
- remote_server1 |
| 127 |
- remote_server2 |
| 128 |
username: DOMAIN\Administrator |
| 129 |
password: some_password |
| 130 |
priority: high |
| 131 |
|
| 132 |
- name: Run PsExec from custom location C:\Program Files\sysinternals\ |
| 133 |
community.windows.win_psexec: |
| 134 |
command: netsh advfirewall set allprofiles state off |
| 135 |
executable: C:\Program Files\sysinternals\psexec.exe |
| 136 |
hostnames: [remote_server] |
| 137 |
password: some_password |
| 138 |
priority: low |
| 139 |
''' |
| 140 |
|
| 141 |
RETURN = r''' |
| 142 |
cmd: |
| 143 |
description: The complete command line used by the module, including PsExec call and additional options. |
| 144 |
returned: always |
| 145 |
type: str |
| 146 |
sample: psexec.exe -nobanner \\remote_server -u "DOMAIN\Administrator" -p "some_password" -accepteula E:\setup.exe |
| 147 |
pid: |
| 148 |
description: The PID of the async process created by PsExec. |
| 149 |
returned: when C(wait=False) |
| 150 |
type: int |
| 151 |
sample: 1532 |
| 152 |
rc: |
| 153 |
description: The return code for the command. |
| 154 |
returned: always |
| 155 |
type: int |
| 156 |
sample: 0 |
| 157 |
stdout: |
| 158 |
description: The standard output from the command. |
| 159 |
returned: always |
| 160 |
type: str |
| 161 |
sample: Success. |
| 162 |
stderr: |
| 163 |
description: The error output from the command. |
| 164 |
returned: always |
| 165 |
type: str |
| 166 |
sample: Error 15 running E:\setup.exe |
| 167 |
''' |