简体   繁体   English

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

[英]Powershell live output from an external command

In our powerscript, I could do something as follows: 在我们的说明书中,我可以做如下的事情:

$output = myexternalcommand
write-output $output

This works. 这有效。 However, the output is displayed only after the external program finishes running. 但是,仅在外部程序完成运行后才会显示输出。 Although the external program is dumping status periodically, there is no indication of what is going on from within the script. 虽然外部程序是定期转储状态,但没有迹象表明脚本中发生了什么。

I am wondering if there is a way to display the output of the external program as it is running. 我想知道是否有办法在外部程序运行时显示它的输出。 Regards. 问候。

Use the Tee-Object to send external command's output to two directions. 使用Tee-Object将外部命令的输出发送到两个方向。 As per the documentation, 根据文档,

The Tee-Object cmdlet redirects output, that is, it sends the output of a command in two directions (like the letter "T"). Tee-Object cmdlet重定向输出,也就是说,它在两个方向上发送命令的输出(如字母“T”)。 It stores the output in a file or variable and also sends it down the pipeline. 它将输出存储在文件或变量中,并将其发送到管道。 If Tee-Object is the last command in the pipeline, the command output is displayed at the prompt . 如果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[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM