![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #5 (permalink) | ||||||||||||
| 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:
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 Specs![]() | |||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #7 (permalink) |
| 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 Specs![]() |
| | #8 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #9 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #10 (permalink) | ||||||||||||
| 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:
| ||||||||||||
My System Specs![]() | |||||||||||||
![]() |
| 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 |