Register-EventObject 在监听进程时不更新控制台

Chr*_*ath 5 powershell powershell-3.0 powershell-5.0

我目前正在编写进程包装器,我正在尝试将 stdout 和 stderr 通道重定向到 powershell 控制台

下面的代码是我用来调用我的进程的函数,但我似乎遇到的问题是我没有从事件处理程序获得任何输出来更新控制台

最后输出和 err 输出正常,但不会在发生时更新

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

} 
Run Code Online (Sandbox Code Playgroud)

Clu*_*fin 1

取消代码中这些行的注释,它应该开始工作:

$process.BeginOutputReadLine()
$process.BeginErrorReadLine()
Run Code Online (Sandbox Code Playgroud)

我修改Write-ErrorWrite-Host的原因与此处讨论的类似:Write-Error does not print to console before script exits

用于输出 redir 测试的示例:

Invoke-Executable ping "127.0.0.1"
Run Code Online (Sandbox Code Playgroud)

用于测试错误 redir 的示例:

Invoke-Executable powershell 'import-module nonexistant -ea continue;exit'
Run Code Online (Sandbox Code Playgroud)

完整代码:

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

}
Run Code Online (Sandbox Code Playgroud)