Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - pipe to non-powershell process

Reply
 
Old 08-29-2008   #1 (permalink)
bsdz


 
 

pipe to non-powershell process

Hi

I need to pipe the output from a powershell commandlet to a perl
process. In its simplest form when I try: -

PS> 1..3 | perl -ne 'print "#$_#\n"'

I do not get the expected output, i.e.

#1#
#2#
#3#

Instead the output is

1
2
3

I have tried wrapping the perl command using Invoke-Command but this
didn't seem to help. Please can anyone help me?

Many thanks
Blair

My System SpecsSystem Spec
Old 08-29-2008   #2 (permalink)
Hal Rottenberg


 
 

Re: pipe to non-powershell process

bsdz wrote:
Quote:

> I need to pipe the output from a powershell commandlet to a perl
> process. In its simplest form when I try: -
>
> PS> 1..3 | perl -ne 'print "#$_#\n"'
The short answer is that it doesn't work quite that way. The pipeline is meant
for use with cmdlets and Perl.exe doesn't speak that language. Not to worry, the
syntax required is only slightly different and requires the foreach-object cmdlet.

1..3 | foreach-object { perl -ne 'print "#$_#\n"' }

Just be careful with your quotes and escape characters, those may be a challenge
with Perl especially as it shares a lot of those with PowerShell. I'm afraid
the $_ above won't be expanded as the larger string is within single quotes.
Good luck with that. You can always do double-double-quotes or escape them
with the backtick and so on.

--
Author, Tech Prosaic blog (http://halr9000.com)
Webmaster, Psi (http://psi-im.org)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
My System SpecsSystem Spec
Old 08-30-2008   #3 (permalink)
bsdz


 
 

Re: pipe to non-powershell process

On Aug 30, 3:50*am, Hal Rottenberg <h...@xxxxxx> wrote:
Quote:

> bsdz wrote:
Quote:

> > I need to pipe the output from a powershell commandlet to a perl
> > process. In its simplest form when I try: -
>
Quote:

> > PS> 1..3 | perl -ne 'print "#$_#\n"'
>
> The short answer is that it doesn't work quite that way. *The pipeline is meant
> for use with cmdlets and Perl.exe doesn't speak that language. Not to worry, the
> syntax required is only slightly different and requires the foreach-object cmdlet.
>
> 1..3 | foreach-object { perl -ne 'print "#$_#\n"' }
>
> Just be careful with your quotes and escape characters, those may be a challenge
> with Perl especially as it shares a lot of those with PowerShell. *I'm afraid
> the $_ above won't be expanded as the larger string is within single quotes.
> Good luck with that. * *You can always do double-double-quotes or escape them
> with the backtick and so on.
>
> --
> Author, Tech Prosaic blog (http://halr9000.com)
> Webmaster, Psi (http://psi-im.org)
> Community Director, PowerShellCommunity.org
> Co-host, PowerScripting Podcast (http://powerscripting.net)
Thanks for your suggestion. Unfortunately, I cannot use it since it
invokes a new instance of Perl on each and every line piped through by
powershell. The main drivers for using Perl is it's superior
performance in string handling and although I boiled down my problem
to a simple example with 3 lines; my real problem entails processing
millions of lines - that's ~1h for powershell and ~5m for Perl.

I suspect I could write my own cmdlet that keeps a non-Powershell
process open and sends the powershell pipe stream to this process.
Actually, do you know of any such CmdLet?
My System SpecsSystem Spec
Old 08-30-2008   #4 (permalink)
bsdz


 
 

Re: pipe to non-powershell process

On Aug 30, 11:29*am, bsdz <blai...@xxxxxx> wrote:
Quote:

> On Aug 30, 3:50*am, Hal Rottenberg <h...@xxxxxx> wrote:
>
>
>
Quote:

> > bsdz wrote:
Quote:

> > > I need to pipe the output from a powershell commandlet to a perl
> > > process. In its simplest form when I try: -
>
Quote:
Quote:

> > > PS> 1..3 | perl -ne 'print "#$_#\n"'
>
Quote:

> > The short answer is that it doesn't work quite that way. *The pipeline is meant
> > for use with cmdlets and Perl.exe doesn't speak that language. Not to worry, the
> > syntax required is only slightly different and requires the foreach-object cmdlet.
>
Quote:

> > 1..3 | foreach-object { perl -ne 'print "#$_#\n"' }
>
Quote:

> > Just be careful with your quotes and escape characters, those may be a challenge
> > with Perl especially as it shares a lot of those with PowerShell. *I'm afraid
> > the $_ above won't be expanded as the larger string is within single quotes.
> > Good luck with that. * *You can always do double-double-quotes orescape them
> > with the backtick and so on.
>
Quote:

> > --
> > Author, Tech Prosaic blog (http://halr9000.com)
> > Webmaster, Psi (http://psi-im.org)
> > Community Director, PowerShellCommunity.org
> > Co-host, PowerScripting Podcast (http://powerscripting.net)
>
> Thanks for your suggestion. Unfortunately, I cannot use it since it
> invokes a new instance of Perl on each and every line piped through by
> powershell. The main drivers for using Perl is it's superior
> performance in string handling and although I boiled down my problem
> to a simple example with 3 lines; my real problem entails processing
> millions of lines - that's ~1h for powershell and ~5m for Perl.
>
> I suspect I could write my own cmdlet that keeps a non-Powershell
> process open and sends the powershell pipe stream to this process.
> Actually, do you know of any such CmdLet?
For any future readers of this message. I have devised a workaround. I
created a Powershell function that started the Perl process and
manipulated the stdin and stdout. I am still in the process of seeing
how well it performs. Here it is: -

function Perl-Filter() {
BEGIN {
$si = New-Object System.Diagnostics.ProcessStartInfo
$si.FileName = "C:\perl\bin\perl.exe"
$si.WorkingDirectory = ((Get-Location).Path)
$si.Arguments = @'
-ne "print \"#$_#\n\""
'@
$si.UseShellExecute = $false
$si.RedirectStandardOutput = $true
$si.RedirectStandardInput = $true

$p = [System.Diagnostics.Process]::Start($si)

}
PROCESS {
$p.StandardInput.WriteLine($_)
$p.StandardInput.Flush()

}
END {
$p.StandardInput.Close()
echo $p.StandardOutput.ReadToEnd()
$p.WaitForExit();
}
}

Use as follow: -

1..3 | Perl-Filter
My System SpecsSystem Spec
Old 08-30-2008   #5 (permalink)
Hal Rottenberg


 
 

Re: pipe to non-powershell process

bsdz wrote:
Quote:

> Thanks for your suggestion. Unfortunately, I cannot use it since it
> invokes a new instance of Perl on each and every line piped through by
> powershell.
Of course, and that's expected to be inefficient.
Quote:

> The main drivers for using Perl is it's superior
> performance in string handling and although I boiled down my problem
> to a simple example with 3 lines; my real problem entails processing
> millions of lines - that's ~1h for powershell and ~5m for Perl.
The creators of PowerShell have tons of experience with and respect for Perl.
People such as Jeffrey Snover and Bruce Payette have said so many times in
interviews or print. That being said, I respectfully suggest that you are
finding your task to be (vastly) quicker in Perl because you don't know how to
do it efficiently in PowerShell. You are not comparing apples to apples when
you "shell out" to Perl from within a PowerShell script.

Give us a chance. Why don't you start a new thread and lay out your entire
task. You can even put in the Perl code, I'm sure you'll find people that will
want to port the whole darn thing for you. After all, it's a challenge to show
off to a Perl guy.

Of course, it is entirely possible that Perl is "better" (that being a
subjective term, hence the quotes) at your particular task, but I bet everybody
would love to see it being worked through.
Quote:

> I suspect I could write my own cmdlet that keeps a non-Powershell
> process open and sends the powershell pipe stream to this process.
> Actually, do you know of any such CmdLet?
There are tons of ways to start and manage processes using PowerShell, but I'm
not sure if any will achieve your _current_ goal. See this article:

http://blogs.microsoft.co.il/blogs/s...owershell.aspx

And PowerShell Community Extensions (PSCX) has a Start-Process cmdlet.
http://codeplex.com/PowerShellCX

Please do let us see your Perl code...

--
Author, Tech Prosaic blog (http://halr9000.com)
Webmaster, Psi (http://psi-im.org)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast (http://powerscripting.net)
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Powershell Pipe to Foreach to get indiviudal elements in a string collection. PowerShell
Using pipe in Powershell PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell
strange PowerShell pipe semantics PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46