外主机缓冲吗? [英] Is Out-Host buffering?

查看:43
本文介绍了外主机缓冲吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,我在其中使用 & 运算符调用应用程序.应用程序产生几行命令行输出,下载一些文件,并返回一个字符串:

I ha a function, where I call an application with the & operator. The application produces several line command line output, downloads some files, and returns a string:

& "app.exe" | Out-Host
$var = ...
return $var

似乎只有在 app.exe 终止后,控制台上才会出现 app.exe 产生的输出.用户没有任何正在下载文件的实时信息.有没有办法在 app.exe 运行时持续更新控制台?

It seems, that on the console appears the output produced by app.exe only after app.exe terminates. The user does not have any real time information which file is downloading. Is there a way to continuously update the console when app.exe is running?

推荐答案

许多控制台应用程序缓冲他们的输出流,如果它知道被重定向的话.实际上,这是 C 库的标准行为.所以,缓冲由 app.exe 完成,因为重定向,而不是 Out-Host.

Many console applications buffer theirs output stream, if it known to be redirected. Actually, it is standard behavior of C library. So, buffering done by app.exe, because of redirection, but not by Out-Host.

解决方案是不重定向 app.exe 的输出,即使外部命令重定向.因为你应该知道 精确条件,当PowerShell不重定向控制台应用程序的输出流并将其直接链接到他们自己的输出流时,这将是交互式PowerShell.exe的控制台会议.条件是:

Solution would be to not to redirect output of app.exe, even when outer command redirected. For than you should know exact condition, when PowerShell not redirect output stream of console application and link it directly to their own output stream, which would be console for interactive PowerShell.exe session. The conditions is:

  1. 命令是管道中的最后一项.
  2. 命令通过管道传送到 Out-Default.

解决方案是将命令包装到脚本块中,并将该脚本块通过管道传输到 Out-Default:

Solution would be wrap command into script block, and pipe that script block to Out-Default:

& { & "app.exe" } | Out-Default

另一种解决方案是使用带有 -NoNewWindow-Wait 参数的 Start-Process cmdlet:

The other solution would be to use Start-Process cmdlet with -NoNewWindow and -Wait parameters:

Start-Process "app.exe" -NoNewWindow -Wait

这篇关于外主机缓冲吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆