Debian Codegraph @

test/support/windows-integration/plugins/modules/win_command.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2017, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #Requires -Module Ansible.ModuleUtils.Legacy
7 #Requires -Module Ansible.ModuleUtils.CommandUtil
8 #Requires -Module Ansible.ModuleUtils.FileUtil
9
10 # TODO: add check mode support
11
12 Set-StrictMode -Version 2
13 $ErrorActionPreference = 'Stop'
14
15 $params = Parse-Args $args -supports_check_mode $false
16
17 $raw_command_line = Get-AnsibleParam -obj $params -name "_raw_params" -type "str" -failifempty $true
18 $chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path"
19 $creates = Get-AnsibleParam -obj $params -name "creates" -type "path"
20 $removes = Get-AnsibleParam -obj $params -name "removes" -type "path"
21 $stdin = Get-AnsibleParam -obj $params -name "stdin" -type "str"
22 $output_encoding_override = Get-AnsibleParam -obj $params -name "output_encoding_override" -type "str"
23
24 $raw_command_line = $raw_command_line.Trim()
25
26 $result = @{
27 changed = $true
28 cmd = $raw_command_line
29 }
30
31 if ($creates -and $(Test-AnsiblePath -Path $creates)) {
32 Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
33 }
34
35 if ($removes -and -not $(Test-AnsiblePath -Path $removes)) {
36 Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0}
37 }
38
39 $command_args = @{
40 command = $raw_command_line
41 }
42 if ($chdir) {
43 $command_args['working_directory'] = $chdir
44 }
45 if ($stdin) {
46 $command_args['stdin'] = $stdin
47 }
48 if ($output_encoding_override) {
49 $command_args['output_encoding_override'] = $output_encoding_override
50 }
51
52 $start_datetime = [DateTime]::UtcNow
53 try {
54 $command_result = Run-Command @command_args
55 } catch {
56 $result.changed = $false
57 try {
58 $result.rc = $_.Exception.NativeErrorCode
59 } catch {
60 $result.rc = 2
61 }
62 Fail-Json -obj $result -message $_.Exception.Message
63 }
64
65 $result.stdout = $command_result.stdout
66 $result.stderr = $command_result.stderr
67 $result.rc = $command_result.rc
68
69 $end_datetime = [DateTime]::UtcNow
70 $result.start = $start_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
71 $result.end = $end_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff")
72 $result.delta = $($end_datetime - $start_datetime).ToString("h\:mm\:ss\.ffffff")
73
74 If ($result.rc -ne 0) {
75 Fail-Json -obj $result -message "non-zero return code"
76 }
77
78 Exit-Json $result