Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_psexec.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2017, Dag Wieers (@dagwieers) <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7 #Requires -Module Ansible.ModuleUtils.ArgvParser
8 #Requires -Module Ansible.ModuleUtils.CommandUtil
9
10 # See also: https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx
11
12 $spec = @{
13 options = @{
14 command = @{ type = 'str'; required = $true }
15 executable = @{ type = 'path'; default = 'psexec.exe' }
16 hostnames = @{ type = 'list'; elements = 'str' }
17 username = @{ type = 'str' }
18 password = @{ type = 'str'; no_log = $true }
19 chdir = @{ type = 'path' }
20 wait = @{ type = 'bool'; default = $true }
21 nobanner = @{ type = 'bool'; default = $false }
22 noprofile = @{ type = 'bool'; default = $false }
23 elevated = @{ type = 'bool'; default = $false }
24 limited = @{ type = 'bool'; default = $false }
25 system = @{ type = 'bool'; default = $false }
26 interactive = @{ type = 'bool'; default = $false }
27 session = @{ type = 'int' }
28 priority = @{ type = 'str'; choices = @( 'background', 'low', 'belownormal', 'abovenormal', 'high', 'realtime' ) }
29 timeout = @{ type = 'int' }
30 }
31 }
32
33 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
34
35 $command = $module.Params.command
36 $executable = $module.Params.executable
37 $hostnames = $module.Params.hostnames
38 $username = $module.Params.username
39 $password = $module.Params.password
40 $chdir = $module.Params.chdir
41 $wait = $module.Params.wait
42 $nobanner = $module.Params.nobanner
43 $noprofile = $module.Params.noprofile
44 $elevated = $module.Params.elevated
45 $limited = $module.Params.limited
46 $system = $module.Params.system
47 $interactive = $module.Params.interactive
48 $session = $module.Params.session
49 $priority = $module.Params.Priority
50 $timeout = $module.Params.timeout
51
52 $module.Result.changed = $true
53
54 If (-Not (Get-Command $executable -ErrorAction SilentlyContinue)) {
55 $module.FailJson("Executable '$executable' was not found.")
56 }
57
58 $arguments = [System.Collections.Generic.List`1[String]]@($executable)
59
60 If ($nobanner -eq $true) {
61 $arguments.Add("-nobanner")
62 }
63
64 # Support running on local system if no hostname is specified
65 If ($hostnames) {
66 $hostname_argument = ($hostnames | Sort-Object -Unique) -join ','
67 $arguments.Add("\\$hostname_argument")
68 }
69
70 # Username is optional
71 If ($null -ne $username) {
72 $arguments.Add("-u")
73 $arguments.Add($username)
74 }
75
76 # Password is optional
77 If ($null -ne $password) {
78 $arguments.Add("-p")
79 $arguments.Add($password)
80 }
81
82 If ($null -ne $chdir) {
83 $arguments.Add("-w")
84 $arguments.Add($chdir)
85 }
86
87 If ($wait -eq $false) {
88 $arguments.Add("-d")
89 }
90
91 If ($noprofile -eq $true) {
92 $arguments.Add("-e")
93 }
94
95 If ($elevated -eq $true) {
96 $arguments.Add("-h")
97 }
98
99 If ($system -eq $true) {
100 $arguments.Add("-s")
101 }
102
103 If ($interactive -eq $true) {
104 $arguments.Add("-i")
105 If ($null -ne $session) {
106 $arguments.Add($session)
107 }
108 }
109
110 If ($limited -eq $true) {
111 $arguments.Add("-l")
112 }
113
114 If ($null -ne $priority) {
115 $arguments.Add("-$priority")
116 }
117
118 If ($null -ne $timeout) {
119 $arguments.Add("-n")
120 $arguments.Add($timeout)
121 }
122
123 $arguments.Add("-accepteula")
124
125 $argument_string = Argv-ToString -arguments $arguments
126
127 # Add the command at the end of the argument string, we don't want to escape
128 # that as psexec doesn't expect it to be one arg
129 $argument_string += " $command"
130
131 $start_datetime = [DateTime]::UtcNow
132
133 # Replace password with *PASSWORD_REPLACED* to avoid disclosing sensitive data
134 $toLog = $argument_string
135 if ($password) {
136 $maskedPassword = Argv-ToString $password
137 $toLog = $toLog.Replace($maskedPassword, "*PASSWORD_REPLACED*")
138 }
139
140 $module.Result.psexec_command = $toLog
141
142 $command_result = Run-Command -command $argument_string
143
144 $end_datetime = [DateTime]::UtcNow
145
146 $module.Result.stdout = $command_result.stdout
147 $module.Result.stderr = $command_result.stderr
148
149 If ($wait -eq $true) {
150 $module.Result.rc = $command_result.rc
151 }
152 else {
153 $module.Result.rc = 0
154 $module.Result.pid = $command_result.rc
155 }
156
157 $module.Result.start = $start_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
158 $module.Result.end = $end_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
159 $module.Result.delta = $($end_datetime - $start_datetime).ToString("h\:mm\:ss\.ffffff")
160
161 $module.ExitJson()