Created
May 26, 2016 20:10
-
-
Save MattHodge/57653f4054a5e856cbafffa77fec08bc to your computer and use it in GitHub Desktop.
Start-ProcessWithCapture.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Allows running processes and capturing output. | |
.DESCRIPTION | |
Allows running processes and capturing output. | |
.EXAMPLE | |
Start-ProcessWithCapture -FilePath 'git' -ArgumentList 'pull' -WorkingDirectory $Path -ErrorAction Stop | |
#> | |
function Start-ProcessWithCapture | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
# File Path of the Process to Execute | |
[Parameter(Mandatory=$true)] | |
[string] | |
$FilePath, | |
# Argument List for Process | |
[Parameter(Mandatory=$true)] | |
[string] | |
$ArgumentList, | |
# Argument List for Process | |
[Parameter(Mandatory=$false)] | |
[string] | |
$WorkingDirectory | |
) | |
$pinfo = New-Object System.Diagnostics.ProcessStartInfo | |
if ($PSBoundParameters.ContainsKey('WorkingDirectory')) | |
{ | |
$pinfo.WorkingDirectory = $WorkingDirectory | |
} | |
$pinfo.FileName = $FilePath | |
$pinfo.RedirectStandardError = $true | |
$pinfo.RedirectStandardOutput = $true | |
$pinfo.UseShellExecute = $false | |
$pinfo.Arguments = $ArgumentList | |
$p = New-Object System.Diagnostics.Process | |
$p.StartInfo = $pinfo | |
$p.Start() | Out-Null | |
$p.WaitForExit() | |
$stdout = $p.StandardOutput.ReadToEnd() | |
$stderr = $p.StandardError.ReadToEnd() | |
$output = @{} | |
$output.stdout = $stdout | |
$output.stderr = $stderr | |
$output.exitcode = $p.ExitCode | |
return $output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment