13

I have a lot of PowerShell script. One main, that calls other, child ones. Those PS scripts in their turn call windows CMD scripts, bash scripts and console applications. All these scripts and applications write messages to console. PowerShell scripts, for example, are using Write-Host scriptlet for this purpose.

Question: how can I easely redirect (send) all this console output to some file, while not deafening (canceling) this console output? I want to be able to see whats going on from console output and also have history of messages in log file.

Thanks.

1
  • Why on earth would you use Write-Host if you intend to do something with the output. Write-Host does exactly that – write something on the host. It's out of control of your script after that. You could, however, implement your own host, but I guess that's less fun. Commented Mar 7, 2011 at 6:47

4 Answers 4

6

You can use the tee equivalent of PowerShell : Tee-Object

PS: serverfault.com and/or superuser.com are more suitable for a question like this.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried Tee-Object earlier, but it does not do what I expect. It only saves to file those output that is being written with Write-Output (to stdout) but not with Write-Host. I cannot replace all Write-Host to Write-Output in PS scripts. Also, Tee-Object ignores errors output (stderr). I need all output that I see on console to be redirected to file. And, ideally, without capital scripts logic rewritting
5

You can try Start-Transcript and Stop-Transcript. It has a couple of limitations like not capturing native exe output. It is also global to PowerShell session.

2 Comments

Looks like a combination of Start-Transcript and Tee-Object can grab all the output, but, unfortunately, to different files. No easy way to merge them
I also voted up for Diadistis' answer (Tee-Object), but decided to accept Keith's answer. Actually, final solution consists of hybrid of two answers, as I've mentioned in previous comment. Two separate files isn't actually a big issue for my case.
2

I've found script for grabbing console output: http://gallery.technet.microsoft.com/scriptcenter/e8fbffde-7d95-42d9-81de-5eb3d9c089e0. Script returns HTML to preserve colors.

The only big downside - you must call it at the end of your script to capture all console output it have made.

Comments

1

You'd probably need to write a custom host to do this. It's not a terribly hard thing to do, but it's does require some managed code.

1 Comment

Thanks, Mike. But such solution is not appropriate for my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.