Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在侦听进程时,Register EventObject不更新控制台_Powershell_Powershell 3.0_Powershell 5.0 - Fatal编程技术网

Powershell 在侦听进程时,Register EventObject不更新控制台

Powershell 在侦听进程时,Register EventObject不更新控制台,powershell,powershell-3.0,powershell-5.0,Powershell,Powershell 3.0,Powershell 5.0,我目前正在编写一个进程包装器,并试图将stdout和stderr通道重定向到powershell控制台 下面的代码是我用来调用进程的函数,但我似乎遇到的问题是,我没有从事件处理程序获得任何输出来更新控制台 输出和err输出在结束时很好,但不会随情况而更新 function Invoke-Executable($ExePath, $ExeArgs) { #Setup ProcessInfo $pinfo = New-Object System.Diagnostics

我目前正在编写一个进程包装器,并试图将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" }

} 

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

$process.BeginOutputReadLine()
$process.BeginErrorReadLine()
我将
写入错误
修改为
写入主机
,原因与此处讨论的类似:

用于输出重读测试的示例:

Invoke-Executable ping "127.0.0.1"
用于测试错误重读的示例:

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

}