Skip to content

Commit

Permalink
Invoke-Process.ps1: Return on object instead of Write-Output
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeesonnuva committed Oct 29, 2019
1 parent 37b9592 commit e919e46
Showing 1 changed file with 61 additions and 50 deletions.
111 changes: 61 additions & 50 deletions Processes/Invoke-Process.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<#PSScriptInfo
.VERSION 1.5
.VERSION 2.0
.GUID b787dc5d-8d11-45e9-aeef-5cf3a1f690de
Expand All @@ -27,54 +27,65 @@
param()

function Invoke-Process {
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FilePath,

[Parameter()]
[ValidateNotNullOrEmpty()]
[string[]]$ArgumentList
)

$ErrorActionPreference = 'Stop'

try {
$stdOutTempFile = "$env:TEMP\$((New-Guid).Guid)"
$stdErrTempFile = "$env:TEMP\$((New-Guid).Guid)"

$startProcessParams = @{
FilePath = $FilePath
ArgumentList = $ArgumentList
RedirectStandardError = $stdErrTempFile
RedirectStandardOutput = $stdOutTempFile
Wait = $true;
PassThru = $true;
NoNewWindow = $true;
}
if ($PSCmdlet.ShouldProcess("Process [$($FilePath)]", "Run with args: [$($ArgumentList)]")) {
$cmd = Start-Process @startProcessParams
$cmdOutput = Get-Content -Path $stdOutTempFile -Raw
$cmdError = Get-Content -Path $stdErrTempFile -Raw
if ($cmd.ExitCode -ne 0) {
if ($cmdError) {
throw $cmdError.Trim()
}
if ($cmdOutput) {
throw $cmdOutput.Trim()
}
} else {
if ([string]::IsNullOrEmpty($cmdOutput) -eq $false) {
Write-Output -InputObject $cmdOutput
}
}
}
} catch {
$PSCmdlet.ThrowTerminatingError($_)
} finally {
Remove-Item -Path $stdOutTempFile, $stdErrTempFile -Force -ErrorAction Ignore
}
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FilePath,
[string[]]$ArgumentList
)

$ErrorActionPreference = 'Stop'

try {
$stdOutTempFile = "$env:TEMP\$(( New-Guid ).Guid)"
$stdErrTempFile = "$env:TEMP\$(( New-Guid ).Guid)"

$startProcessParams = @{
FilePath = $FilePath
RedirectStandardError = $stdErrTempFile
RedirectStandardOutput = $stdOutTempFile
Wait = $true
PassThru = $true
NoNewWindow = $true
}
if ($PSCmdlet.ShouldProcess("Process [$($FilePath)]", "Run with args: [$($ArgumentList)]")) {
if ($ArgumentList) {
Write-Verbose -Message "$FilePath $ArgumentList"
$cmd = Start-Process @startProcessParams -ArgumentList $ArgumentList
}
else {
Write-Verbose $FilePath
$cmd = Start-Process @startProcessParams
}
$stdOut = Get-Content -Path $stdOutTempFile -Raw
$stdErr = Get-Content -Path $stdErrTempFile -Raw
if ([string]::IsNullOrEmpty($stdOut) -eq $false) {
$stdOut = $stdOut.Trim()
}
if ([string]::IsNullOrEmpty($stdErr) -eq $false) {
$stdErr = $stdErr.Trim()
}
$return = [PSCustomObject]@{
Name = $cmd.Name
Id = $cmd.Id
ExitCode = $cmd.ExitCode
Output = $stdOut
Error = $stdErr
}
if ($return.ExitCode -ne 0) {
throw $return
}
else {
$return
}
}
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
finally {
Remove-Item -Path $stdOutTempFile, $stdErrTempFile -Force -ErrorAction Ignore
}
}

0 comments on commit e919e46

Please sign in to comment.