header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Redirection in Powershell console
Last Post 19 May 2008 01:59 PM by bruceatk. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
bruceatkUser is Offline
New Member
New Member
Posts:12

--
18 May 2008 04:54 PM  

I have to create/maintain a lot of batch processes that are the glue for keeping diverse systems working together.  I currently use lots of different tools to get things done and I'm looking to PowerShell to help me simplify what I do.

One thing that I do is make small specific purpose scripts and programs that read and write using stdio.  I have been able to configure powershell so that when run from cmd.exe or a batch file to redirect it's output.  I can use things like this from cmd.exe:

.\GrabSomeData.ps1 | more

.\GrabSomeData.ps1 >saveforlater.txt

It seems that I should be able to do these things from the Powershell console, but I can't.

.\GrabSomeData.ps1 >saveforlater.txt

when the above  is run from PowerShell, it displays the output to the screen.  Some output will be redirected (ie. measure-command) but the output I care about doesn't get redirected.  I'm even using $host.UI.WriteLine and it doesn't get redirected from the PowerShell console.

I like to redirect using stdio because it gives flexibility.  If I create small useful things, I can then rearrange them and put them back together easily in a batch file, script, or command line  without having to rewrite them.

Bruce

halr9000User is Offline
Basic Member
Basic Member
Posts:303

--
19 May 2008 02:19 AM  
There's actually several streams with Powershell, and in v1 there's a few gaps in how things work. For example, see this thread on the powershell newsgroup.

As far as other ways to address your issue, I'd suggest doing everything from within a powershell session, rather than from cmd with bath files. Any reason you can't do that?
bruceatkUser is Offline
New Member
New Member
Posts:12

--
19 May 2008 03:53 AM  
I want to do it from a powershell session, but it doesn't work for me. It works fine from cmd.exe. I tried 1> to 9> in powershell and the only output that is redirected is my measure-command. (write-host, $host.ui.writeline, etc) just go to the screen when in a powershell console.

Bruce
halr9000User is Offline
Basic Member
Basic Member
Posts:303

--
19 May 2008 12:40 PM  
Ok, the problem here is in fact write-host. Host equals the screen and that's where the output goes. If you like the redirect technique, then you need to be using write-output everywhere instead. Write-Output basically means "write to STDOUT".
bruceatkUser is Offline
New Member
New Member
Posts:12

--
19 May 2008 01:11 PM  

Thanks.

I think the help should be clearer about what the different outputs really are. I found this article by Keith Hill which explains quite a bit about the problem:

http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!811.entry


The Write-Output doesn't perform as well as $Host.ui.WriteLine, effectively doubling the time that my test script takes to run. If I capture the output into a string and then use fewer Write-Outputs I can get the performance back to where I need it.

Bruce

halr9000User is Offline
Basic Member
Basic Member
Posts:303

--
19 May 2008 01:31 PM  
We actually just had Keith Hill on the podcast and he brings up the topic of difficulties in dealing with multiple output streams. :) Very smart guy.
bruceatkUser is Offline
New Member
New Member
Posts:12

--
19 May 2008 01:59 PM  
Thanks for doing the podcast. I started listening about 3 weeks ago. I've been working through them from the beginning. I'm up to #22.

Bruce
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer