| 1 |
# Copyright (c) 2017 Ansible Project |
| 2 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 3 |
|
| 4 |
#AnsibleRequires -CSharpUtil Ansible.Process |
| 5 |
|
| 6 |
Function Get-ExecutablePath { |
| 7 |
<# |
| 8 |
.SYNOPSIS |
| 9 |
Gets the full path to an executable, will search the directory specified or ones in the PATH env var. |
| 10 |
|
| 11 |
.PARAMETER executable |
| 12 |
[String]The executable to search for. |
| 13 |
|
| 14 |
.PARAMETER directory |
| 15 |
[String] If set, the directory to search in. |
| 16 |
|
| 17 |
.OUTPUT |
| 18 |
[String] The full path the executable specified. |
| 19 |
#> |
| 20 |
Param( |
| 21 |
[String]$executable, |
| 22 |
[String]$directory = $null |
| 23 |
) |
| 24 |
|
| 25 |
# we need to add .exe if it doesn't have an extension already |
| 26 |
if (-not [System.IO.Path]::HasExtension($executable)) { |
| 27 |
$executable = "$($executable).exe" |
| 28 |
} |
| 29 |
$full_path = [System.IO.Path]::GetFullPath($executable) |
| 30 |
|
| 31 |
if ($full_path -ne $executable -and $directory -ne $null) { |
| 32 |
$file = Get-Item -LiteralPath "$directory\$executable" -Force -ErrorAction SilentlyContinue |
| 33 |
} |
| 34 |
else { |
| 35 |
$file = Get-Item -LiteralPath $executable -Force -ErrorAction SilentlyContinue |
| 36 |
} |
| 37 |
|
| 38 |
if ($null -ne $file) { |
| 39 |
$executable_path = $file.FullName |
| 40 |
} |
| 41 |
else { |
| 42 |
$executable_path = [Ansible.Process.ProcessUtil]::SearchPath($executable) |
| 43 |
} |
| 44 |
return $executable_path |
| 45 |
} |
| 46 |
|
| 47 |
Function Run-Command { |
| 48 |
<# |
| 49 |
.SYNOPSIS |
| 50 |
Run a command with the CreateProcess API and return the stdout/stderr and return code. |
| 51 |
|
| 52 |
.PARAMETER command |
| 53 |
The full command, including the executable, to run. |
| 54 |
|
| 55 |
.PARAMETER working_directory |
| 56 |
The working directory to set on the new process, will default to the current working dir. |
| 57 |
|
| 58 |
.PARAMETER stdin |
| 59 |
A string to sent over the stdin pipe to the new process. |
| 60 |
|
| 61 |
.PARAMETER environment |
| 62 |
A hashtable of key/value pairs to run with the command. If set, it will replace all other env vars. |
| 63 |
|
| 64 |
.PARAMETER output_encoding_override |
| 65 |
The character encoding name for decoding stdout/stderr output of the process. |
| 66 |
|
| 67 |
.OUTPUT |
| 68 |
[Hashtable] |
| 69 |
[String]executable - The full path to the executable that was run |
| 70 |
[String]stdout - The stdout stream of the process |
| 71 |
[String]stderr - The stderr stream of the process |
| 72 |
[Int32]rc - The return code of the process |
| 73 |
#> |
| 74 |
Param( |
| 75 |
[string]$command, |
| 76 |
[string]$working_directory = $null, |
| 77 |
[string]$stdin = "", |
| 78 |
[hashtable]$environment = @{}, |
| 79 |
[string]$output_encoding_override = $null |
| 80 |
) |
| 81 |
|
| 82 |
# need to validate the working directory if it is set |
| 83 |
if ($working_directory) { |
| 84 |
# validate working directory is a valid path |
| 85 |
if (-not (Test-Path -LiteralPath $working_directory)) { |
| 86 |
throw "invalid working directory path '$working_directory'" |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
# lpApplicationName needs to be the full path to an executable, we do this |
| 91 |
# by getting the executable as the first arg and then getting the full path |
| 92 |
$arguments = [Ansible.Process.ProcessUtil]::ParseCommandLine($command) |
| 93 |
$executable = Get-ExecutablePath -executable $arguments[0] -directory $working_directory |
| 94 |
|
| 95 |
# run the command and get the results |
| 96 |
$command_result = [Ansible.Process.ProcessUtil]::CreateProcess($executable, $command, $working_directory, $environment, $stdin, $output_encoding_override) |
| 97 |
|
| 98 |
return , @{ |
| 99 |
executable = $executable |
| 100 |
stdout = $command_result.StandardOut |
| 101 |
stderr = $command_result.StandardError |
| 102 |
rc = $command_result.ExitCode |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
# this line must stay at the bottom to ensure all defined module parts are exported |
| 107 |
Export-ModuleMember -Function Get-ExecutablePath, Run-Command |