win_psexec: Rewrite using AnsibleModule (#48383)

This commit is contained in:
Dag Wieers 2018-11-11 22:00:57 +01:00 committed by Jordan Borean
parent 62332155b5
commit d174ebc65e

View file

@ -3,38 +3,58 @@
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <dag@wieers.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#Requires -Module Ansible.ModuleUtils.Legacy
#AnsibleRequires -CSharpUtil Ansible.Basic
#Requires -Module Ansible.ModuleUtils.ArgvParser
#Requires -Module Ansible.ModuleUtils.CommandUtil
# See also: https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx
$params = Parse-Args $args
$command = Get-AnsibleParam -obj $params -name "command" -type "str" -failifempty $true
$executable = Get-AnsibleParam -obj $params -name "executable" -type "path" -default "psexec.exe"
$hostnames = Get-AnsibleParam -obj $params -name "hostnames" -type "list"
$username = Get-AnsibleParam -obj $params -name "username" -type "str"
$password = Get-AnsibleParam -obj $params -name "password" -type "str"
$chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path"
$wait = Get-AnsibleParam -obj $params -name "wait" -type "bool" -default $true
$nobanner = Get-AnsibleParam -obj $params -name "nobanner" -type "bool" -default $false
$noprofile = Get-AnsibleParam -obj $params -name "noprofile" -type "bool" -default $false
$elevated = Get-AnsibleParam -obj $params -name "elevated" -type "bool" -default $false
$limited = Get-AnsibleParam -obj $params -name "limited" -type "bool" -default $false
$system = Get-AnsibleParam -obj $params -name "system" -type "bool" -default $false
$interactive = Get-AnsibleParam -obj $params -name "interactive" -type "bool" -default $false
$session = Get-AnsibleParam -obj $params -name "session" -type "int"
$priority = Get-AnsibleParam -obj $params -name "priority" -type "str" -validateset "background","low","belownormal","abovenormal","high","realtime"
$timeout = Get-AnsibleParam -obj $params -name "timeout" -type "int"
$extra_opts = Get-AnsibleParam -obj $params -name "extra_opts" -type "list"
$result = @{
changed = $true
$spec = @{
options = @{
command = @{ type='str'; required=$true }
executable = @{ type='path'; default='psexec.exe' }
hostnames = @{ type='list' }
username = @{ type='str' }
password = @{ type='str'; no_log=$true }
chdir = @{ type='path' }
wait = @{ type='bool'; default=$true }
nobanner = @{ type='bool'; default=$false }
noprofile = @{ type='bool'; default=$false }
elevated = @{ type='bool'; default=$false }
limited = @{ type='bool'; default=$false }
system = @{ type='bool'; default=$false }
interactive = @{ type='bool'; default=$false }
session = @{ type='int' }
priority = @{ type='str'; choices=@( 'background', 'low', 'belownormal', 'abovenormal', 'high', 'realtime' ) }
timeout = @{ type='int' }
extra_opts = @{ type='list' }
}
}
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
$command = $module.Params.command
$executable = $module.Params.executable
$hostnames = $module.Params.hostnames
$username = $module.Params.username
$password = $module.Params.password
$chdir = $module.Params.chdir
$wait = $module.Params.wait
$nobanner = $module.Params.nobanner
$noprofile = $module.Params.noprofile
$elevated = $module.Params.elevated
$limited = $module.Params.limited
$system = $module.Params.system
$interactive = $module.Params.interactive
$session = $module.Params.session
$priority = $module.Params.Priority
$timeout = $module.Params.timeout
$extra_opts = $module.Params.extra_opts
$module.Result.changed = $true
If (-Not (Get-Command $executable -ErrorAction SilentlyContinue)) {
Fail-Json $result "Executable '$executable' was not found."
$module.FailJson("Executable '$executable' was not found.")
}
$arguments = @()
@ -113,29 +133,29 @@ $arguments += "-accepteula"
$argument_string = Argv-ToString -arguments $arguments
# add the command at the end of the argument string, we don't want to escape
# Add the command at the end of the argument string, we don't want to escape
# that as psexec doesn't expect it to be one arg
$argument_string += " $command"
$start_datetime = [DateTime]::UtcNow
$result.psexec_command = "$executable $argument_string"
$module.Result.psexec_command = "$executable $argument_string"
$command_result = Run-Command -command "$executable $argument_string"
$end_datetime = [DateTime]::UtcNow
$result.stdout = $command_result.stdout
$result.stderr = $command_result.stderr
$module.Result.stdout = $command_result.stdout
$module.Result.stderr = $command_result.stderr
If ($wait -eq $true) {
$result.rc = $command_result.rc
$module.Result.rc = $command_result.rc
} else {
$result.rc = 0
$result.pid = $command_result.rc
$module.Result.rc = 0
$module.Result.pid = $command_result.rc
}
$result.start = $start_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
$result.end = $end_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
$result.delta = $($end_datetime - $start_datetime).ToString("h\:mm\:ss\.ffffff")
$module.Result.start = $start_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
$module.Result.end = $end_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
$module.Result.delta = $($end_datetime - $start_datetime).ToString("h\:mm\:ss\.ffffff")
Exit-Json $result
$module.ExitJson()