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. 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"). 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.
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[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With