ps1.soapyfrog.com

doing inappropriate things with powershell

Pages

  • About
  • Downloads
  • Archives

Search

Popular Posts

  • Grrr 1.1 and Big Invaders
  • Space Invaders
  • Convert images to text (ASCII art)
  • Cmdlet clashes
  • Console screen grabs in html

Recent Posts

  • Grrr source code, including Invaders
  • Google going down the pan
  • Cmdlet clashes
  • Grrr 1.1 and Big Invaders
  • Grrr, Cmdlets and PSInvaders revival

Categories

  • Announce (7)
  • Cmdlets (2)
  • Cool (16)
  • Grrr (6)
  • Hint (2)
  • Invaders (5)
  • Odd (2)
  • PowerShell (27)
  • Quiz (3)
  • Rant (7)
  • Uncategorized (1)
  • Utility (5)

Months

  • August 2007 (1)
  • April 2007 (1)
  • March 2007 (1)
  • February 2007 (3)
  • January 2007 (25)
  • December 2006 (1)

Bookmarks

  • Blogroll
    • $script Fanatics
    • blog.soapyfrog.com
    • Brian Long
    • Lee Holmes
    • Nik Crabtree
    • PowerShell-Scripting (French)
    • Richy Rich
    • The PowerShell Guy
    • Windows PowerShell
  • Links
    • Carbon-neutral web hosting!

Meta

  • Log in
  • Posts RSS
  • Comments RSS
  • Valid XHTML
  • Valid CSS
« Hashes and properties
Alternate Data Streams »
 

Pipe or no pipe?

17 Jan 2007 02:50 pm// Cool, PowerShell, Quiz    

Many built-in cmdlets take input from the pipe, but alternatively let you specify an input with the -InputObject parameter. An example is the Get-Member cmdlet.

I want to be able to do this with my own functions, but as far as I can tell, there is no built-in mechanism for this for using an specified parameter instead of the pipe.

[Updated]

What I do is this:

# A simple function that multiplies input items by 3
function times3 {
  param($i)
  # this inner function does the actual processing
  function proc($o) {
      $o * 3
  }
  # if -i supplied used instead of std $input
  if ($i) { $input = $i }
  foreach ($o in $input) {
    proc $o
  }
}

Normally, you’d use a function with a process block to process each object ($_) in the pipeline, but in this case, I iterate over the pipeline’s $input manually. If the -i is supplied, $input is replaced with its value.

If I run this sequence:

$debugpreference="Continue"
write-debug "----------- about to call as function"
times3

write-debug "----------- about to pipe 4 numbers in to function"
1..4 | times3

write-debug "----------- about to call as function with -i"
times3 -i (1..4)

I get this:

PS Documents\\proj\\ps1> ./pipeornot.ps1
DEBUG: ----------- about to call as function
DEBUG: ----------- about to pipe 4 numbers in to function
3
6
9
12
DEBUG: ----------- about to call as function with -in
3
6
9
12
PS Documents\\proj\\ps1> get-bufferhtml > out.html

It would be nice if the the manual condition could be avoided by specifying

param($i=$input)

but sadly, this does not work.

Maybe in the next version of PowerShell, or perhaps there will be a more specific way of dealing with pipe or no pipe.

2 comments to “Pipe or no pipe?”

  1. On 15 Feb 2007 at 2:27 pm, Daniele Muscetta said:   

    This is because you want to do it from your scripts and/or functions…

    Many “cmdlets” accept this, as you said, and in fact, you can write a cmdlet rather than a function that behaves this way, just like the other cmdlets. Start from here:

    http://msdn2.microsoft.com/en-us/library/ms714663.aspx

    PS - Keep up the great work - PSInvaders is AWESOME, you know ??

  2. On 15 Feb 2007 at 2:36 pm, adrian said:   

    Thanks. I’m currently writing a whole load of cmdlets so know about things like [Parameter(Position=0,ValueFromPipe=true)] and so on.

    It’s a shame that that sort of thing couldn’t be exposed in PowerShell language itself.

Copyright © 2006-2008 Adrian Milliner

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.