I am currently in the process of writing a process wrapper and i am trying to redirect the stdout, and stderr channels to the powershell console
the following code is the function I am using to invoke my process but the problem i seem to be having is that i don't get any output from the event handlers to update the console
the output and err output fine at the end but will not update as it happens
function Invoke-Executable($ExePath, $ExeArgs)
{
#Setup ProcessInfo
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $ExePath
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $ExeArgs
#Setup Process
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $pinfo
#Setup Error Listener
$errEvent = Register-ObjectEvent -InputObj $process `
-Event "ErrorDataReceived" `
-Action `
{
param
(
[System.Object] $sender,
[System.Diagnostics.DataReceivedEventArgs] $e
)
Write-Error $e.Data
}
#Setup Out Listener
$outEvent = Register-ObjectEvent -InputObj $process `
-Event "OutputDataReceived" `
-Action `
{
param
(
[System.Object] $sender,
[System.Diagnostics.DataReceivedEventArgs] $e
)
Write-Host $e.Data
}
# Start the process
[Void] $process.Start()
# Begin async read events
# $process.BeginOutputReadLine()
# $process.BeginErrorReadLine()
while (!$process.HasExited)
{
Start-Sleep -Milliseconds 250
Write-Host "ping"
}
$stdout = $process.StandardOutput.ReadToEnd()
$stderr = $process.StandardError.ReadToEnd()
# if ($stdout) {Write-Host "$stdout"}
# if ($stderr) { Write-Error "$stderr" }
}
Uncomment these lines in your code and it should start working:
$process.BeginOutputReadLine()
$process.BeginErrorReadLine()
I modified Write-Error
to Write-Host
for reasons similar to what is discussed here: Write-Error does not print to console before script exits
Example used for output redir testing :
Invoke-Executable ping "127.0.0.1"
Example used for testing error redir :
Invoke-Executable powershell 'import-module nonexistant -ea continue;exit'
Full code :
function Invoke-Executable($ExePath, $ExeArgs)
{
#Setup ProcessInfo
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = $ExePath
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $ExeArgs
#Setup Process
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $pinfo
#Setup Error Listener
$errEvent = Register-ObjectEvent -InputObj $process `
-Event "ErrorDataReceived" `
-Action `
{
param
(
[System.Object] $sender,
[System.Diagnostics.DataReceivedEventArgs] $e
)
Write-host $e.Data
}
#Setup Out Listener
$outEvent = Register-ObjectEvent -InputObj $process `
-Event "OutputDataReceived" `
-Action `
{
param
(
[System.Object] $sender,
[System.Diagnostics.DataReceivedEventArgs] $e
)
Write-Host $e.Data
}
# Start the process
[Void] $process.Start()
# Begin async read events
$process.BeginOutputReadLine()
$process.BeginErrorReadLine()
while (!$process.HasExited)
{
Start-Sleep -Milliseconds 250
Write-Host "ping"
}
# if ($stdout) {Write-Host "$stdout"}
# if ($stderr) { Write-Error "$stderr" }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With