Powershell来自外部命令的实时输出

时间:2015-08-24 02:00:14

标签: powershell

在我们的说明书中,我可以做如下的事情:

$output = myexternalcommand
write-output $output

这很有效。但是,仅在外部程序完成运行后才会显示输出。虽然外部程序是定期转储状态,但没有迹象表明脚本内发生了什么。

我想知道是否有办法在外部程序运行时显示它的输出。问候。

1 个答案:

答案 0 :(得分:3)

使用Tee-Object将外部命令的输出发送到两个方向。根据文档,

  

Tee-Object cmdlet重定向输出,即发送输出   两个方向的命令(如字母" T")。它存储了   输出文件或变量,并将其发送到管道。 如果   Tee-Object是管道中的最后一个命令,命令输出是   在提示中显示

cmd /c "dir /s c:\windows\system32" | Tee-Object -Variable foobar
# dir list is printed and result is also stored in $foobar
$foobar.Count # will return the dir cmd's output as an Object[]