View Single Post
Old 12-08-2006   #7 (permalink)
Lee Holmes [MSFT]


 
 

Re: Lee Holmes' blog: TCP in PSH

Hi Marco;

If you want to use the script interactively, but also have it write the
output to a file, this is what I would do:

1) Add an optional filename parameter to the script so that your script can
know when the user (you) want to redirect into a file as well. You can use
the way that the script handles the $inputObject parameter as an example of
this.
2) In the part that writes the line to the host (write-host $line,) add a
line below it that appends that line to a file as well (if the user
specified a filename.) That way, you'll still get the visual feedback, but
will have the contents also going into a file.

--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"Marco Shaw" <marco@Znbnet.nb.ca> wrote in message
news:OV9er3mGHHA.3616@TK2MSFTNGP06.phx.gbl...
> Now, instead of outputting to the host, I'd like to ouput to a string or
> file for later processing.
>
> With version 1, this seemed more obvious to accomplish:
>
> Change this part:
> while($stream.DataAvailable)
> {
> $read = $stream.Read($buffer, 0, 4096)
> write-host -n ($encoding.GetString($buffer, 0, $read))
> }
>
> To something like:
> while($stream.DataAvailable)
> {
> $read = $stream.Read($buffer, 0, 4096)
> write-output ($encoding.GetString($buffer, 0,
> $read))|out-file -append -encoding ascii -filepath some_log_file
> }
>
> I'm sure my problem with my test though is that the buffer was just too
> small. I did get output written to some_log_file! But the problem also
> occured that I didn't have any feedback as to when I could enter my 2nd
> command.
>
> I'm assuming that I could just use this to output to a string, but haven't
> tried it:
> while($stream.DataAvailable)
> {
> $read = $stream.Read($buffer, 0, 4096)
> $out_data+=($encoding.GetString($buffer, 0, $read))
> }
>
>
> With version 2 though, I'd like to use your scripting functionality, but
> I'm not experienced enough in PSH to figure out what's going on, and
> determine where I need to send the output to a string or file.
>
> Can you help me?
>
> Marco
>



My System SpecsSystem Spec