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

标签 powershell powershell-3.0 powershell-5.0

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

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

输出和错误输出在最后很好,但不会更新

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 修改为 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" }

}

关于powershell - Register-EventObject 在监听进程时不更新控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42100338/

相关文章:

powershell - 如何在 PowerShell 中运行 appCmd 以将自定义 header 添加到默认网站

windows - Powershell 调用命令的问题

regex - 替换 powershell 中的特殊字符

database - 我想在 red hat linux 服务器上执行一个 .ps1 powershell 脚本

powershell - 如何从大量文件夹中删除单个用户 ACL?

powershell - 组合哈希表时 PowerShell 的行为

powershell - 在Powershell 5.0类中使用自定义类型

arrays - 在PowerShell中针对大型阵列的大量正则表达式删除非常慢

Powershell 错误提示库不可用稍后再试

java - 在linux上编译运行HelloWorld.java