![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | redirect write-host How do I redirect the output of a script that uses write-host? Ie I have a script Foo.ps1 write-host Hello and I have tried Foo > foo.log Foo | tee foo.log If I change the write-host to write-output, then I can redirect the output, but then when I use the tee, the output does not appear on the console until the script completes (I have a long running script and I want to see the progress of the script displayed immediately on the console) Rgds Douglas Woods |
My System Specs![]() |
| | #2 (permalink) |
| | Re: redirect write-host Try to play with Start-Transcript, perhaps it is what you want (more or less). -- Thanks, Roman Kuzmin |
My System Specs![]() |
| | #3 (permalink) |
| | Re: redirect write-host Thanks, a new command for me :-) Yes that does do the job, but would still oike to know if it is possible to redirect output from write-host. -- Douglas Woods "Roman Kuzmin" wrote: > Try to play with Start-Transcript, perhaps it is what you want (more or > less). > > -- > Thanks, > Roman Kuzmin > > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: redirect write-host I dont believe you can redirect write-host... use write-output instead. Why do you want to use write-host? If it is because of the color/layout... I would suggest making a function that takes input and pipes it to write-host and write-output or you could just do both like this write-host "hello";write-output "hello" | out-file testme.txt "DouglasWoods" <DouglasWoods@discussions.microsoft.com> wrote in message news:70012569-B8F5-4ABC-A09D-72D4CBE9E097@microsoft.com... > Thanks, a new command for me :-) Yes that does do the job, but would still > oike to know if it is possible to redirect output from write-host. > -- > Douglas Woods > > > "Roman Kuzmin" wrote: > >> Try to play with Start-Transcript, perhaps it is what you want (more or >> less). >> >> -- >> Thanks, >> Roman Kuzmin >> >> >> |
My System Specs![]() |
| | #5 (permalink) |
| | Re: redirect write-host "DouglasWoods" <DouglasWoods@discussions.microsoft.com> wrote in message news:70012569-B8F5-4ABC-A09D-72D4CBE9E097@microsoft.com... > Thanks, a new command for me :-) Yes that does do the job, but would still > oike to know if it is possible to redirect output from write-host. I don't know of a way but I need this capability too. When we used Korn shell we used a construct like this: exec 2>&1 foo.log After that *everything* gets redirected to foo.log. In PowerShell almost every interesting line has this appended to it: >> foo.log That makes the script very noisy and hard to read. Before anybody says well why don' t you just redirect to foo.log when you run the file. That doesn't seem very well encapsulated to me. For years we've written our scripts to generate their own log files generally named the same thing as the script but with a .log extension rather than .ksh. What I would love to see is a similar facility to Korn shell's exec 2>&1. In fact, I would like to hook all output to the host so I can redirect it to a log file even output from cmdlets like Write-Host/Warning/Error. -- Keith |
My System Specs![]() |
| | #6 (permalink) |
| | Re: redirect write-host Keith, 2>&1 just redirects stderr and stdout. I think the problem here is that write-host doesn't output to stdout. I have seen some apps (sysinternals ones if I recall) that cannot be redirected either. This may just be a Windows thing. I discussed this with /\/\0\/\/ awhile back and I think our solution was a function that encapsulates all the write-x and use it. Im not at work so I dont have it handy, but something like function write-stuff { param($msg,$output,$log,[switch]$both) if($output -eq "host") { Write-Host $msg } if($output -eq "out") { Write-Output $msg } if($log) { $msg | out-file $log -enc ASCII -append } if($both) { Write-Host $msg Write-Output $msg } } I wrote this on the bus so no testing, but I think you get my drift. p.s. I, like yourself, would like to see this functionality native, but I think I can see why they did it and I decided to use this workaround. "Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message news:FDCD75BA-CEA9-41BA-AB3D-202EE109D370@microsoft.com... > "DouglasWoods" <DouglasWoods@discussions.microsoft.com> wrote in message > news:70012569-B8F5-4ABC-A09D-72D4CBE9E097@microsoft.com... >> Thanks, a new command for me :-) Yes that does do the job, but would >> still >> oike to know if it is possible to redirect output from write-host. > > I don't know of a way but I need this capability too. When we used Korn > shell we used a construct like this: > > exec 2>&1 foo.log > > After that *everything* gets redirected to foo.log. In PowerShell almost > every interesting line has this appended to it: > >>> foo.log > > That makes the script very noisy and hard to read. Before anybody says > well why don' t you just redirect to foo.log when you run the file. That > doesn't seem very well encapsulated to me. For years we've written our > scripts to generate their own log files generally named the same thing as > the script but with a .log extension rather than .ksh. What I would love > to see is a similar facility to Korn shell's exec 2>&1. In fact, I would > like to hook all output to the host so I can redirect it to a log file > even output from cmdlets like Write-Host/Warning/Error. > > -- > Keith |
My System Specs![]() |
| | #7 (permalink) |
| | Re: redirect write-host "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message news:%23XNUaMahHHA.4140@TK2MSFTNGP05.phx.gbl... > Keith, 2>&1 just redirects stderr and stdout. I think the problem here is > that write-host doesn't output to stdout. In Korn shell "exec 2>&1 foo.log" redirects *everything* to foo.log. Of course, there is only stdout and stderr in Korn shell to redirect. Still, this is a very convenient construct to have and one I would like to see in PowerShell and extended to capture all host output. > p.s. I, like yourself, would like to see this functionality native, but I > think I can see why they did it and I decided to use this workaround. I imagine it was just one of those things that wasn't deemed necessary for a v1.0 release (which I agree with). Let's hope they can get this in the next release. -- Keith |
My System Specs![]() |
| | #8 (permalink) |
| | Re: redirect write-host another tip Write-Error gets logged in $error also, you can check it (or dumpt to file ) after the script has runned PoSH> Write-Error 'foo' Write-Error 'foo' : foo PoSH> $Error Write-Error 'foo' : foo not exactly what was asked can be handy also in this case Greetings /\/\o\/\/ "Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message news:A19D5ECF-42EF-439C-B026-59964EC08C62@microsoft.com... > "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message > news:%23XNUaMahHHA.4140@TK2MSFTNGP05.phx.gbl... >> Keith, 2>&1 just redirects stderr and stdout. I think the problem here is >> that write-host doesn't output to stdout. > > In Korn shell "exec 2>&1 foo.log" redirects *everything* to foo.log. Of > course, there is only stdout and stderr in Korn shell to redirect. Still, > this is a very convenient construct to have and one I would like to see in > PowerShell and extended to capture all host output. > >> p.s. I, like yourself, would like to see this functionality native, but I >> think I can see why they did it and I decided to use this workaround. > > I imagine it was just one of those things that wasn't deemed necessary for > a v1.0 release (which I agree with). Let's hope they can get this in the > next release. > > -- > Keith |
My System Specs![]() |
| | #9 (permalink) |
| | Re: redirect write-host "/\/\o\/\/ [MVP]" <mow001@hotmail.NoSpam> wrote in message news:B6C85496-ABE3-4DCD-B68C-C3A073A154C7@microsoft.com... > another tip Write-Error gets logged in $error also, you can check it (or > dumpt to file ) after the script has runned > > PoSH> Write-Error 'foo' > Write-Error 'foo' : foo > > PoSH> $Error > Write-Error 'foo' : foo > > not exactly what was asked can be handy also in this case Hi Mow, since $Error is a global wouldn't it contain error information from other stuff besides just the execution of a particular script i.e. like errors you encountered before you ran the script? -- Keith |
My System Specs![]() |
| | #10 (permalink) |
| | Re: redirect write-host I have some functions that, when I use Write-Host, the function no longer outputs naked text. In other functions, the problem does not occur. I'd love to get to the bottom of this. I'm aware of Write- Error and Write-Warning, but Write-Host is so much better, because it only writes what you tell it to write, in exactly the color you specify. And I like to use green for the final success. Thanks, Mike |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Custom PSHostUserInterface and write-host | PowerShell | |||
| write-host? no thanks | PowerShell | |||
| When to use write-host? | PowerShell | |||
| 1..9|%{write-host $_:443} | PowerShell | |||
| Write-Host question | PowerShell | |||