Register-EventObject does not update the console while listening to the process

I'm currently writing process wrappers and I'm trying to redirect stdout and stderr feeds to powershell console

The following code is a function that I use to invoke my process, but the problem I seem to encounter is that I am not getting any output from the event handlers for updating the console.

output and erroneous output stop at the end, but will not be updated 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" }

} 
+4
source share
1 answer

Uncomment these lines in the code and it should start working:

$process.BeginOutputReadLine()
$process.BeginErrorReadLine()

Write-Error Write-Host , , : script

, :

Invoke-Executable ping "127.0.0.1"

, redir:

Invoke-Executable powershell 'import-module nonexistant -ea continue;exit'

:

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" }

}
+1

Source: https://habr.com/ru/post/1669085/


All Articles