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

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

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

输出和错误输出最后但不会更新

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

} 

最佳答案 在代码中取消注释这些行,它应该开始工作:

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

我修改了Write-Error to Write-Host,其原因类似于此处讨论的内容:Write-Error does not print to console before script exits

用于输出redir测试的示例:

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

}
点赞