我正在使用PowerShell脚本来执行控制台应用程序,我试图从那里重定向标准输出和标准错误。 我使用的代码如下:PowerShell,流在运行外部进程时处理输出和错误
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$ProcessInfo.FileName = "myExe.exe"
$ProcessInfo.Arguments = "bla bla bla"
$ProcessInfo.RedirectStandardError = $true
$ProcessInfo.RedirectStandardOutput = $true
$ProcessInfo.UseShellExecute = $false
$Process = New-Object System.Diagnostics.Process
$Process.StartInfo = $ProcessInfo
$Process.Start() | Out-Null
$output = $Process.StandardOutput.ReadToEnd()
$errors = $Process.StandardError.ReadToEnd()
$Process.WaitForExit()
$output
$errors
return $Process.ExitCode
到目前为止好,如果我有一个错误,我可以看到它重定向到我PowerShell控制台,如果我有它输出也被重新定向。 问题是这个过程需要10分钟,同时我们不知道发生了什么。
有没有什么办法可以在PowerShell中我可以流输出和错误的内容,当进程正在运行?在纯.NET中,我们可以订阅Process类的事件,我可以在PowerShell中执行相同的操作吗?
只是一个快速的评论。不要考虑在进程中调用'WaitForExit',因为直到进程完成后才会生成输出。 –