2017-09-13 16:58:49 +00:00
|
|
|
# Copyright (c) 2017 Ansible Project
|
2017-11-15 22:43:34 +00:00
|
|
|
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
2017-09-13 16:58:49 +00:00
|
|
|
|
2018-12-13 01:15:25 +00:00
|
|
|
#AnsibleRequires -CSharpUtil Ansible.Process
|
2017-09-13 16:58:49 +00:00
|
|
|
|
2018-12-13 01:15:25 +00:00
|
|
|
Function Load-CommandUtils {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
No-op, as the C# types are automatically loaded.
|
|
|
|
#>
|
|
|
|
Param()
|
|
|
|
$msg = "Load-CommandUtils is deprecated and no longer needed, this cmdlet will be removed in a future version"
|
|
|
|
if ((Get-Command -Name Add-DeprecationWarning -ErrorAction SilentlyContinue) -and (Get-Variable -Name result -ErrorAction SilentlyContinue)) {
|
|
|
|
Add-DeprecationWarning -obj $result.Value -message $msg -version 2.12
|
|
|
|
} else {
|
|
|
|
$module = Get-Variable -Name module -ErrorAction SilentlyContinue
|
|
|
|
if ($null -ne $module -and $module.Value.GetType().FullName -eq "Ansible.Basic.AnsibleModule") {
|
|
|
|
$module.Value.Deprecate($msg, "2.12")
|
2017-09-13 16:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 01:15:25 +00:00
|
|
|
Function Get-ExecutablePath {
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Get's the full path to an executable, will search the directory specified or ones in the PATH env var.
|
2017-09-13 16:58:49 +00:00
|
|
|
|
2018-12-13 01:15:25 +00:00
|
|
|
.PARAMETER executable
|
|
|
|
[String]The executable to seach for.
|
2018-05-22 21:12:32 +00:00
|
|
|
|
2018-12-13 01:15:25 +00:00
|
|
|
.PARAMETER directory
|
|
|
|
[String] If set, the directory to search in.
|
2018-05-22 21:12:32 +00:00
|
|
|
|
2018-12-13 01:15:25 +00:00
|
|
|
.OUTPUT
|
|
|
|
[String] The full path the executable specified.
|
|
|
|
#>
|
|
|
|
Param(
|
|
|
|
[String]$executable,
|
|
|
|
[String]$directory = $null
|
|
|
|
)
|
2017-09-13 16:58:49 +00:00
|
|
|
|
|
|
|
# we need to add .exe if it doesn't have an extension already
|
|
|
|
if (-not [System.IO.Path]::HasExtension($executable)) {
|
|
|
|
$executable = "$($executable).exe"
|
|
|
|
}
|
|
|
|
$full_path = [System.IO.Path]::GetFullPath($executable)
|
|
|
|
|
|
|
|
if ($full_path -ne $executable -and $directory -ne $null) {
|
2019-03-15 09:44:53 +00:00
|
|
|
$file = Get-Item -LiteralPath "$directory\$executable" -Force -ErrorAction SilentlyContinue
|
2017-09-13 16:58:49 +00:00
|
|
|
} else {
|
2019-03-15 09:44:53 +00:00
|
|
|
$file = Get-Item -LiteralPath $executable -Force -ErrorAction SilentlyContinue
|
2017-09-13 16:58:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 09:44:53 +00:00
|
|
|
if ($null -ne $file) {
|
2017-09-13 16:58:49 +00:00
|
|
|
$executable_path = $file.FullName
|
|
|
|
} else {
|
2018-12-13 01:15:25 +00:00
|
|
|
$executable_path = [Ansible.Process.ProcessUtil]::SearchPath($executable)
|
2017-09-13 16:58:49 +00:00
|
|
|
}
|
|
|
|
return $executable_path
|
|
|
|
}
|
|
|
|
|
|
|
|
Function Run-Command {
|
2018-12-13 01:15:25 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Run a command with the CreateProcess API and return the stdout/stderr and return code.
|
|
|
|
|
|
|
|
.PARAMETER command
|
|
|
|
The full command, including the executable, to run.
|
|
|
|
|
|
|
|
.PARAMETER working_directory
|
|
|
|
The working directory to set on the new process, will default to the current working dir.
|
|
|
|
|
|
|
|
.PARAMETER stdin
|
|
|
|
A string to sent over the stdin pipe to the new process.
|
|
|
|
|
|
|
|
.PARAMETER environment
|
|
|
|
A hashtable of key/value pairs to run with the command. If set, it will replace all other env vars.
|
|
|
|
|
|
|
|
.OUTPUT
|
|
|
|
[Hashtable]
|
|
|
|
[String]executable - The full path to the executable that was run
|
|
|
|
[String]stdout - The stdout stream of the process
|
|
|
|
[String]stderr - The stderr stream of the process
|
|
|
|
[Int32]rc - The return code of the process
|
|
|
|
#>
|
2017-09-13 16:58:49 +00:00
|
|
|
Param(
|
2018-12-13 01:15:25 +00:00
|
|
|
[string]$command,
|
|
|
|
[string]$working_directory = $null,
|
|
|
|
[string]$stdin = "",
|
|
|
|
[hashtable]$environment = @{}
|
2017-09-13 16:58:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# need to validate the working directory if it is set
|
|
|
|
if ($working_directory) {
|
|
|
|
# validate working directory is a valid path
|
2019-03-15 09:44:53 +00:00
|
|
|
if (-not (Test-Path -LiteralPath $working_directory)) {
|
2017-09-13 16:58:49 +00:00
|
|
|
throw "invalid working directory path '$working_directory'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# lpApplicationName needs to be the full path to an executable, we do this
|
|
|
|
# by getting the executable as the first arg and then getting the full path
|
2018-12-13 01:15:25 +00:00
|
|
|
$arguments = [Ansible.Process.ProcessUtil]::ParseCommandLine($command)
|
2017-09-13 16:58:49 +00:00
|
|
|
$executable = Get-ExecutablePath -executable $arguments[0] -directory $working_directory
|
|
|
|
|
|
|
|
# run the command and get the results
|
2018-12-13 01:15:25 +00:00
|
|
|
$command_result = [Ansible.Process.ProcessUtil]::CreateProcess($executable, $command, $working_directory, $environment, $stdin)
|
2017-09-13 16:58:49 +00:00
|
|
|
|
|
|
|
return ,@{
|
|
|
|
executable = $executable
|
2017-09-16 06:09:15 +00:00
|
|
|
stdout = $command_result.StandardOut
|
|
|
|
stderr = $command_result.StandardError
|
|
|
|
rc = $command_result.ExitCode
|
2017-09-13 16:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# this line must stay at the bottom to ensure all defined module parts are exported
|
2018-12-13 01:15:25 +00:00
|
|
|
Export-ModuleMember -Function Get-ExecutablePath, Load-CommandUtils, Run-Command
|