Huddled Masses
You can do more than breathe for free...
Browse: Home / Writing Better Script Functions for the PowerShell Pipeline

Writing Better Script Functions for the PowerShell Pipeline

By Joel 'Jaykul' Bennett on 25-Oct-2007

I wrote a post last week about how to write functions for use in the PowerShell pipeline and I’ve been using the template I wrote in that post as the basis for several of my other scripts … and I’ve been gradually fleshing it out, and improving it, so I thought I’d drop it here with all of it’s inline comments. Hopefully that will be better than what you would get if I just trying to explain it in a blog post :) .

Incidentally, this script is also on PowerShellCentral scripts Repository where I will probably post any future modifications …


## A Template for functions which can _also_ be executed in the pipeline ....
##   by Joel Bennett, in hopes it will help...
## Version History
## v1.0 First public release (after over 9 different versions in my various other functions)
## v1.2 Show the use of Write-Output, and change "return" in the BEGIN to "Write-Output" to avoid
##      the pooling of the output from the process block when it's invoked as a function.
## v1.3 Switched back to "break" instead of "return" so that if you pass via the pipeline AND via
##      the inputObject, only the inputObject gets process (this is how cmdlets behave).
###################################################################################################
## The simplest thing is to make -inputObject the very last argument and make sure that you name
## each parameter that you expect to see ... as always you'll have to specify a value for each
## parameter, or specifically name the inputObject parameter.
##

function Test-Pipeline($Parameter1=$null, $inputObject=$null) {
     BEGIN {
      if ($inputObject) {
         ### If you're accepting $args, you need to pass those in...
         # Write-Output $io | &($MyInvocation.InvocationName) $args;
         Write-Output $inputObject | &($MyInvocation.InvocationName) $Parameter1;
         break;
      }
      else
      {  ## DO ONCE: (on the re-invoke when using -inputObject)
         Write-host "Begin $Parameter1 $args"
      }
   }
   PROCESS {
      ## You have to at least make sure it's got a value
      ## Really you should check it's TYPE to make sure you can do something useful...
      if($_) {
         Write-Host "Write-HOST: $_"
         ## You should make a practice of explicitly calling Write-Output on things
         ## That's how you emit them into the pipeline instead of just printing them
         Write-Output "Write-OUTPUT $_"
         ## Most of the time, piping to Out-Default is a lot like using Write-Host...
         "Out-Default $_" | Out-Default
      }
   }
   END {
      if (-not $inputObject)
      {  ## DO ONCE: (on the re-invoke when using -inputObject)
         $x = 0
         $args | % { $x++; "$x - $_" }
        Write-host "End"
      }
   }
}

###################################################################################################
## Sample call:
##
## [1]> Test-Pipeline Foo! @("one", "two", "three")
## Begin Foo!
## Write-HOST: one
## Write-OUTPUT one
## Out-Default one
## Write-HOST: two
## Write-OUTPUT two
## Out-Default two
## Write-HOST: three
## Write-OUTPUT three
## Out-Default three
## End

## [2]> "one", "two", "three" | Test-Pipeline Foo!
## Begin Foo!
## Write-HOST: one
## Write-OUTPUT one
## Out-Default one
## Write-HOST: two
## Write-OUTPUT two
## Out-Default two
## Write-HOST: three
## Write-OUTPUT three
## Out-Default three
## End

## ## ## To demonstrate the point of output, assign the value, or pipe it into something...
## [3]> $pipeline = "one", "two", "three" | Test-Pipeline Foo!
## Begin Foo!
## Write-HOST: one
## Out-Default one
## Write-HOST: two
## Out-Default two
## Write-HOST: three
## Out-Default three
## End

Similar Posts:

  • More Custom Attributes for PowerShell (Parameter Transformation)
  • Rich formatting for PowerShell help
  • Better error messages for PowerShell ValidatePattern
  • Parenthesis in PowerShell
  • PowerShell 3 – Finally on the DLR!

Posted in Huddled | Tagged Pipeline, PowerShell, PowerShell Functions, Scripting

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011
  • November 2010
  • August 2010

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.