| 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 |
# Cleanse CLIXML from stderr (sift out error stream data, discard others for now) |
| 16 |
Function Cleanse-Stderr($raw_stderr) { |
| 17 |
Try { |
| 18 |
# NB: this regex isn't perfect, but is decent at finding CLIXML amongst other stderr noise |
| 19 |
If($raw_stderr -match "(?s)(?<prenoise1>.*)#< CLIXML(?<prenoise2>.*)(?<clixml><Objs.+</Objs>)(?<postnoise>.*)") { |
| 20 |
$clixml = [xml]$matches["clixml"] |
| 21 |
|
| 22 |
$merged_stderr = "{0}{1}{2}{3}" -f @( |
| 23 |
$matches["prenoise1"], |
| 24 |
$matches["prenoise2"], |
| 25 |
# filter out just the Error-tagged strings for now, and zap embedded CRLF chars |
| 26 |
($clixml.Objs.ChildNodes | Where-Object { $_.Name -eq 'S' } | Where-Object { $_.S -eq 'Error' } | ForEach-Object { $_.'#text'.Replace('_x000D__x000A_','') } | Out-String), |
| 27 |
$matches["postnoise"]) | Out-String |
| 28 |
|
| 29 |
return $merged_stderr.Trim() |
| 30 |
|
| 31 |
# FUTURE: parse/return other streams |
| 32 |
} |
| 33 |
Else { |
| 34 |
$raw_stderr |
| 35 |
} |
| 36 |
} |
| 37 |
Catch { |
| 38 |
"***EXCEPTION PARSING CLIXML: $_***" + $raw_stderr |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
$params = Parse-Args $args -supports_check_mode $false |
| 43 |
|
| 44 |
$raw_command_line = Get-AnsibleParam -obj $params -name "_raw_params" -type "str" -failifempty $true |
| 45 |
$chdir = Get-AnsibleParam -obj $params -name "chdir" -type "path" |
| 46 |
$executable = Get-AnsibleParam -obj $params -name "executable" -type "path" |
| 47 |
$creates = Get-AnsibleParam -obj $params -name "creates" -type "path" |
| 48 |
$removes = Get-AnsibleParam -obj $params -name "removes" -type "path" |
| 49 |
$stdin = Get-AnsibleParam -obj $params -name "stdin" -type "str" |
| 50 |
$no_profile = Get-AnsibleParam -obj $params -name "no_profile" -type "bool" -default $false |
| 51 |
$output_encoding_override = Get-AnsibleParam -obj $params -name "output_encoding_override" -type "str" |
| 52 |
|
| 53 |
$raw_command_line = $raw_command_line.Trim() |
| 54 |
|
| 55 |
$result = @{ |
| 56 |
changed = $true |
| 57 |
cmd = $raw_command_line |
| 58 |
} |
| 59 |
|
| 60 |
if ($creates -and $(Test-AnsiblePath -Path $creates)) { |
| 61 |
Exit-Json @{msg="skipped, since $creates exists";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0} |
| 62 |
} |
| 63 |
|
| 64 |
if ($removes -and -not $(Test-AnsiblePath -Path $removes)) { |
| 65 |
Exit-Json @{msg="skipped, since $removes does not exist";cmd=$raw_command_line;changed=$false;skipped=$true;rc=0} |
| 66 |
} |
| 67 |
|
| 68 |
$exec_args = $null |
| 69 |
If(-not $executable -or $executable -eq "powershell") { |
| 70 |
$exec_application = "powershell.exe" |
| 71 |
|
| 72 |
# force input encoding to preamble-free UTF8 so PS sub-processes (eg, Start-Job) don't blow up |
| 73 |
$raw_command_line = "[Console]::InputEncoding = New-Object Text.UTF8Encoding `$false; " + $raw_command_line |
| 74 |
|
| 75 |
# Base64 encode the command so we don't have to worry about the various levels of escaping |
| 76 |
$encoded_command = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($raw_command_line)) |
| 77 |
|
| 78 |
if ($stdin) { |
| 79 |
$exec_args = "-encodedcommand $encoded_command" |
| 80 |
} else { |
| 81 |
$exec_args = "-noninteractive -encodedcommand $encoded_command" |
| 82 |
} |
| 83 |
|
| 84 |
if ($no_profile) { |
| 85 |
$exec_args = "-noprofile $exec_args" |
| 86 |
} |
| 87 |
} |
| 88 |
Else { |
| 89 |
# FUTURE: support arg translation from executable (or executable_args?) to process arguments for arbitrary interpreter? |
| 90 |
$exec_application = $executable |
| 91 |
if (-not ($exec_application.EndsWith(".exe"))) { |
| 92 |
$exec_application = "$($exec_application).exe" |
| 93 |
} |
| 94 |
$exec_args = "/c $raw_command_line" |
| 95 |
} |
| 96 |
|
| 97 |
$command = "`"$exec_application`" $exec_args" |
| 98 |
$run_command_arg = @{ |
| 99 |
command = $command |
| 100 |
} |
| 101 |
if ($chdir) { |
| 102 |
$run_command_arg['working_directory'] = $chdir |
| 103 |
} |
| 104 |
if ($stdin) { |
| 105 |
$run_command_arg['stdin'] = $stdin |
| 106 |
} |
| 107 |
if ($output_encoding_override) { |
| 108 |
$run_command_arg['output_encoding_override'] = $output_encoding_override |
| 109 |
} |
| 110 |
|
| 111 |
$start_datetime = [DateTime]::UtcNow |
| 112 |
try { |
| 113 |
$command_result = Run-Command @run_command_arg |
| 114 |
} catch { |
| 115 |
$result.changed = $false |
| 116 |
try { |
| 117 |
$result.rc = $_.Exception.NativeErrorCode |
| 118 |
} catch { |
| 119 |
$result.rc = 2 |
| 120 |
} |
| 121 |
Fail-Json -obj $result -message $_.Exception.Message |
| 122 |
} |
| 123 |
|
| 124 |
# TODO: decode CLIXML stderr output (and other streams?) |
| 125 |
$result.stdout = $command_result.stdout |
| 126 |
$result.stderr = Cleanse-Stderr $command_result.stderr |
| 127 |
$result.rc = $command_result.rc |
| 128 |
|
| 129 |
$end_datetime = [DateTime]::UtcNow |
| 130 |
$result.start = $start_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff") |
| 131 |
$result.end = $end_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff") |
| 132 |
$result.delta = $($end_datetime - $start_datetime).ToString("h\:mm\:ss\.ffffff") |
| 133 |
|
| 134 |
If ($result.rc -ne 0) { |
| 135 |
Fail-Json -obj $result -message "non-zero return code" |
| 136 |
} |
| 137 |
|
| 138 |
Exit-Json $result |