Skip to content

Instantly share code, notes, and snippets.

@indented-automation
Last active July 15, 2022 12:33
Show Gist options
  • Save indented-automation/fba795c43ef5a53483398cdc72ab7fa0 to your computer and use it in GitHub Desktop.
Save indented-automation/fba795c43ef5a53483398cdc72ab7fa0 to your computer and use it in GitHub Desktop.
function Invoke-NativeCommand {
<#
.SYNOPSIS
Invoke a native command (.exe) as a new process.
.DESCRIPTION
Invoke-NativeCommand executes an arbitrary executable as a new process. Both the standard
and error output streams are redirected.
Error out is written as a single non-terminating error. ErrorAction can be used to raise
this as a terminating error.
.EXAMPLE
Invoke-NativeCommand git clone repo-uri -ErrorAction Stop
Run the git command to clone repo-uri. Raise a terminating error if the command fails.
#>
[CmdletBinding()]
param (
<#
The command line to execute. This parameter is named to attempt to avoid conflicts with
parameters for the executing command line.
#>
[Parameter(Position = 1, ValueFromRemainingArguments, ValueFromPipeline)]
$__CommandLine
)
process {
$command, $argumentList = $__CommandLine
try {
$process = [System.Diagnostics.Process]@{
StartInfo = [System.Diagnostics.ProcessStartInfo]@{
FileName = (Get-Command $command -ErrorAction Stop).Source
Arguments = $argumentList
WorkingDirectory = $pwd
RedirectStandardOutput = $true
RedirectStandardError = $true
UseShellExecute = $false
}
}
$null = $process.Start()
$process.WaitForExit()
while (-not $process.StandardOutput.EndOfStream) {
$process.StandardOutput.ReadToEnd()
}
while (-not $process.StandardError.EndOfStream) {
Write-Error $process.StandardError.ReadToEnd()
}
} catch {
Write-Error -ErrorRecord $_
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment