Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Detecting when a function is running in a pipeline?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-10-2008   #1 (permalink)
catweazle9
Guest


 

Detecting when a function is running in a pipeline?

I've written the following function to call "grep -i" (Cygwin is on my
path) over the string form of the provided input:
function ggi { $input | out-string | grep -i $args[0] }

... and the following work:
PS C:\> ls | ggi bat
-a--- 18/09/2006 22:43 24 autoexec.bat
PS C:\> gps | ggi firef
367 17 202984 222684 322 614.22 5028 firefox

However, I'd also like to be able to use this function outside the
pipeline, e.g.,:
ggi sometext *.bat

I think I need to update the function to detect when there is no
$input (i.e., the function is not running in a pipeline) and then just
pass all the args directly to grep, but I'm a bit stumped because
$input has type SZArrayEnumerator.

Ah, I've just discovered I can use
if ("$input" -eq "")

Is this the recommended way to do it? The PowerShell in Action book
states "When a function is used in a pipeline, a special variable
$input is available...", which suggests there might be some way to
test whether or not the $input variable is "available" within the
scope of the currently executing function?

Thanks,

John.

My System SpecsSystem Spec
Old 01-10-2008   #2 (permalink)
Brandon Shell [MVP]
Guest


 

Re: Detecting when a function is running in a pipeline?

Two things

1) Check out Select-String
http://technet.microsoft.com/en-us/l.../bb978678.aspx

This should do what your looking for
2) function ggi {if($input.count -gt 0){$input | out-string | grep -i $args[0]}else{cat
$args[0] | grep -i $args[1]}}

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
Quote:

> I've written the following function to call "grep -i" (Cygwin is on my
> path) over the string form of the provided input:
> function ggi { $input | out-string | grep -i $args[0] }
> .. and the following work:
> PS C:\> ls | ggi bat
> -a--- 18/09/2006 22:43 24 autoexec.bat
> PS C:\> gps | ggi firef
> 367 17 202984 222684 322 614.22 5028 firefox
> However, I'd also like to be able to use this function outside the
> pipeline, e.g.,:
> ggi sometext *.bat
> I think I need to update the function to detect when there is no
> $input (i.e., the function is not running in a pipeline) and then just
> pass all the args directly to grep, but I'm a bit stumped because
> $input has type SZArrayEnumerator.
>
> Ah, I've just discovered I can use
> if ("$input" -eq "")
> Is this the recommended way to do it? The PowerShell in Action book
> states "When a function is used in a pipeline, a special variable
> $input is available...", which suggests there might be some way to
> test whether or not the $input variable is "available" within the
> scope of the currently executing function?
>
> Thanks,
>
> John.
>

My System SpecsSystem Spec
Old 01-10-2008   #3 (permalink)
catweazle9
Guest


 

Re: Detecting when a function is running in a pipeline?

Thanks for the suggestion, although it didn't quite work for me
because $input.count returns an empty string (SZArrayEnumerator has no
Count method). I searched for SZArrayEnumerator in this newsgroup and
found that using @ seems to accumulate the objects from the enumerator
into an array that then does have a Count method. I've now got this,
which seems to work:

function ggi {
if (@($input).count -eq 0) {
grep -i $args
}
else {
$input.reset()
$input | out-string | grep -i $args
}
}
My System SpecsSystem Spec
Old 01-10-2008   #4 (permalink)
Brandon Shell [MVP]
Guest


 

Re: Detecting when a function is running in a pipeline?

if you do @($input) it will stop the pipe until it process it all... it would
be best to use the process{} section

function ggi {
process{if($_){$_ | out-string | grep -i $args[0]}}
end{if($args.count -gt 1){cat $args[0] | grep -i $args[1]}}
}

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
Quote:

> Thanks for the suggestion, although it didn't quite work for me
> because $input.count returns an empty string (SZArrayEnumerator has no
> Count method). I searched for SZArrayEnumerator in this newsgroup and
> found that using @ seems to accumulate the objects from the enumerator
> into an array that then does have a Count method. I've now got this,
> which seems to work:
>
> function ggi {
> if (@($input).count -eq 0) {
> grep -i $args
> }
> else {
> $input.reset()
> $input | out-string | grep -i $args
> }
> }

My System SpecsSystem Spec
Old 01-10-2008   #5 (permalink)
catweazle9
Guest


 

Re: Detecting when a function is running in a pipeline?

On Jan 10, 5:14 pm, Brandon Shell [MVP] <a_bshell.m...@xxxxxx>
wrote:
Quote:

> if you do @($input) it will stop the pipe until it process it all... it would
> be best to use the process{} section
>
> function ggi {
> process{if($_){$_ | out-string | grep -i $args[0]}}
> end{if($args.count -gt 1){cat $args[0] | grep -i $args[1]}}
>
> }
Hmm, if I use this updated version, it takes excessively long (around
6 seconds on my machine) to execute the following:
ls c:/windows | ggi exe

... whereas the same command seems to execuate instantaneously when
using my function above.

Also your updated version fails when not using a pipe:

PS C:\> ggi2 foo autoexec.bat
Get-Content : Cannot find path 'C:\foo' because it does not exist.
At line:3 char:30
+ end{if($args.count -gt 1){cat <<<< $args[0] | grep -i $args[1]}}
PS C:\>

John.
My System SpecsSystem Spec
Old 01-10-2008   #6 (permalink)
Brandon Shell [MVP]
Guest


 

Re: Detecting when a function is running in a pipeline?

Curious...

It works for me (fast.) Perhaps is a path issue.

btw.. have you look at select-string. I believe it does what you want?


Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
Quote:

> On Jan 10, 5:14 pm, Brandon Shell [MVP] <a_bshell.m...@xxxxxx>
> wrote:
>
Quote:

>> if you do @($input) it will stop the pipe until it process it all...
>> it would be best to use the process{} section
>>
>> function ggi {
>> process{if($_){$_ | out-string | grep -i $args[0]}}
>> end{if($args.count -gt 1){cat $args[0] | grep -i $args[1]}}
>> }
>>
> Hmm, if I use this updated version, it takes excessively long (around
> 6 seconds on my machine) to execute the following:
> ls c:/windows | ggi exe
> .. whereas the same command seems to execuate instantaneously when
> using my function above.
>
> Also your updated version fails when not using a pipe:
>
> PS C:\> ggi2 foo autoexec.bat
> Get-Content : Cannot find path 'C:\foo' because it does not exist.
> At line:3 char:30
> + end{if($args.count -gt 1){cat <<<< $args[0] | grep -i $args[1]}}
> PS C:\>
> John.
>

My System SpecsSystem Spec
Old 01-10-2008   #7 (permalink)
catweazle9
Guest


 

Re: Detecting when a function is running in a pipeline?

For the 2nd problem, the args just needed to be swaped:
cat $args[0] | grep -i $args[1]}
cat $args[1] | grep -i $args[0]}

As far as the slowdown, I don't know why it's so noticeable. However
this is virtually instantaneous:
ls c:/windows | grep exe
... whereas this takes a surprising 6 seconds (the directory has only
88 files):
ls c:/windows | %{ ($_ | out-string | grep exe )}

What exactly is PowerShell doing in this case for it to take so long?
If I remove the "grep" from the pipeline, it's still slow, but that
might be the time it takes to write all the text to the console...

John.
My System SpecsSystem Spec
Old 01-10-2008   #8 (permalink)
Brandon Shell [MVP]
Guest


 

Re: Detecting when a function is running in a pipeline?

I am not sure why it is slower... perhaps someone else would be able to shed
some light on that.

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject
Quote:

> For the 2nd problem, the args just needed to be swaped:
> cat $args[0] | grep -i $args[1]}
> cat $args[1] | grep -i $args[0]}
> As far as the slowdown, I don't know why it's so noticeable. However
> this is virtually instantaneous:
> ls c:/windows | grep exe
> .. whereas this takes a surprising 6 seconds (the directory has only
> 88 files):
> ls c:/windows | %{ ($_ | out-string | grep exe )}
> What exactly is PowerShell doing in this case for it to take so long?
> If I remove the "grep" from the pipeline, it's still slow, but that
> might be the time it takes to write all the text to the console...
>
> John.
>

My System SpecsSystem Spec
Old 01-10-2008   #9 (permalink)
Shay Levi
Guest


 

Re: Detecting when a function is running in a pipeline?


The $_, current pipeline object, becomes available inside the pipeline so
you can check for its existence.


function ggi{
process{
if($_) { "pipelined" } else { "regular function call" }
}
}


PS > "power","shell" | ggi
pipelined
pipelined

PS > ggi "powershell"
regular function call




-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> I've written the following function to call "grep -i" (Cygwin is on my
> path) over the string form of the provided input:
> function ggi { $input | out-string | grep -i $args[0] }
> .. and the following work:
> PS C:\> ls | ggi bat
> -a--- 18/09/2006 22:43 24 autoexec.bat
> PS C:\> gps | ggi firef
> 367 17 202984 222684 322 614.22 5028 firefox
> However, I'd also like to be able to use this function outside the
> pipeline, e.g.,:
> ggi sometext *.bat
> I think I need to update the function to detect when there is no
> $input (i.e., the function is not running in a pipeline) and then just
> pass all the args directly to grep, but I'm a bit stumped because
> $input has type SZArrayEnumerator.
>
> Ah, I've just discovered I can use
> if ("$input" -eq "")
> Is this the recommended way to do it? The PowerShell in Action book
> states "When a function is used in a pipeline, a special variable
> $input is available...", which suggests there might be some way to
> test whether or not the $input variable is "available" within the
> scope of the currently executing function?
>
> Thanks,
>
> John.
>

My System SpecsSystem Spec
Old 01-10-2008   #10 (permalink)
Karl Prosser[MVP]
Guest


 

Re: Detecting when a function is running in a pipeline?

How many files in the directory.. the foreach-object % , does have a bit
of an overhead and there has to be some scafolding set up, and its
running the scriptblock each time.. which in this case is calling grep
so many times as there are files, rather than once.. and maybe grep has
a 1/4 second latency or something?

-dunno

catweazle9@xxxxxx wrote:
Quote:

> For the 2nd problem, the args just needed to be swaped:
> cat $args[0] | grep -i $args[1]}
> cat $args[1] | grep -i $args[0]}
>
> As far as the slowdown, I don't know why it's so noticeable. However
> this is virtually instantaneous:
> ls c:/windows | grep exe
> .. whereas this takes a surprising 6 seconds (the directory has only
> 88 files):
> ls c:/windows | %{ ($_ | out-string | grep exe )}
>
> What exactly is PowerShell doing in this case for it to take so long?
> If I remove the "grep" from the pipeline, it's still slow, but that
> might be the time it takes to write all the text to the console...
>
> John.
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Not Detecting an IDE HDD in Windows NeoDeGenero Drivers 3 08-18-2008 07:40 PM
System is not detecting Mic Shilps Vista hardware & devices 1 02-26-2008 03:44 AM
Vista not detecting CD-RW guywilson Vista hardware & devices 14 06-01-2007 12:37 PM
2 GB of Ram only detecting 1 GB =?Utf-8?B?ZGJ3c3I=?= Vista hardware & devices 4 08-14-2006 11:50 AM
BUG: Redirecting function contents to a file truncates function lines at the width of the console Adam Milazzo PowerShell 2 08-11-2006 10:58 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51