Created
September 7, 2022 12:59
-
-
Save potatoqualitee/db686934c6eb04417ef9354b6d8e13c0 to your computer and use it in GitHub Desktop.
Invoke-Program
This file contains 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
function Invoke-Program { | |
[CmdletBinding()] | |
[OutputType([System.Management.Automation.PSObject])] | |
param | |
( | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[string]$FilePath, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string]$ArgumentList, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[uint32[]]$SuccessReturnCodes = @(0, 3010), | |
[switch]$EnableException | |
) | |
process { | |
try { | |
$output = [pscustomobject]@{ | |
ComputerName = $env:COMPUTERNAME | |
Path = $Path | |
ArgumentList = $ArgumentList | |
WorkingDirectory = $WorkingDirectory | |
Successful = $false | |
stdout = $null | |
stderr = $null | |
ExitCode = $null | |
} | |
$command = Get-Command $FilePath | Select-Object -ExpandProperty Source | |
#npx --help | |
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo | |
$processStartInfo.FileName = $command | |
if ($ArgumentList) { | |
$processStartInfo.Arguments = $ArgumentList | |
if ($ExpandStrings) { | |
$processStartInfo.Arguments = $ExecutionContext.InvokeCommand.ExpandString($ArgumentList) | |
} | |
} | |
if ($WorkingDirectory) { | |
$processStartInfo.WorkingDirectory = $WorkingDirectory | |
if ($ExpandStrings) { | |
$processStartInfo.WorkingDirectory = $ExecutionContext.InvokeCommand.ExpandString($WorkingDirectory) | |
} | |
} | |
$processStartInfo.UseShellExecute = $false # This is critical for installs to function on core servers | |
$processStartInfo.CreateNoWindow = $true | |
$processStartInfo.RedirectStandardError = $true | |
$processStartInfo.RedirectStandardOutput = $true | |
$ps = New-Object System.Diagnostics.Process | |
$ps.StartInfo = $processStartInfo | |
$started = $ps.Start() | |
if ($started) { | |
$stdOut = $ps.StandardOutput.ReadToEnd() | |
$stdErr = $ps.StandardError.ReadToEnd() | |
$ps.WaitForExit() | |
# assign output object values | |
$output.stdout = $stdOut | |
$output.stderr = $stdErr | |
$output.ExitCode = $ps.ExitCode | |
# Check the exit code of the process to see if it succeeded. | |
if ($ps.ExitCode -in $SuccessReturnCode) { | |
$output.Successful = $true | |
} | |
$output | |
} | |
} catch { | |
Stop-PSFFunction -EnableException:$EnableException -Message "Failed to invoke program '$FilePath'." -ErrorRecord $PSItem | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment