<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; PowerShell Functions</title>
	<atom:link href="http://huddledmasses.org/tag/functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerShell: Determine your function&#8217;s position in the pipeline</title>
		<link>http://huddledmasses.org/powershell-determine-your-functions-position-in-the-pipeline/</link>
		<comments>http://huddledmasses.org/powershell-determine-your-functions-position-in-the-pipeline/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 02:54:33 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1362</guid>
		<description><![CDATA[I just posted an article to the FAQ on the PoshCode Wiki answering this question that came up again on our IRC PowerShell user group today. It came up in the context of determining whether a function was the last function on the pipeline, because one of our users was looking for a way (other [...]]]></description>
			<content:encoded><![CDATA[	<p>I just posted an article to the <a href="http://wiki.poshcode.org/index.php?title=FAQ/Tips_and_Tricks/How_Do_I..._Determine_My_Function%27s_Position_in_the_Pipeline"><span class="caps">FAQ</span> on the PoshCode Wiki</a> answering this question that came up again on our <span class="caps">IRC</span> <a href="http://PowerShellGroup.org/virtual">PowerShell user group</a> today. It came up in the context of determining whether a function was the last function on the pipeline, because one of our users was looking for a way (other than creating ps1xml files) to output objects onto the pipeline for use in other functions, but still format those objects nicely if they were output directly. </p>

	<p>Before I give the solution, I just want to say: <strong>don&#8217;t change your output based on where you are in a pipeline.</strong></p>

	<p>There are numerous scenarios where your function will be the last one on a pipeline, but still be participating in further pipelines, including formatting and output modification. For example, take our function Test-Pipeline (defined below) in these three scenarios below. In none of these scenarios would it be appropriate for the function to write formatted output instead of outputting the raw object, but in each case, the function <strong>is</strong> the last function in the pipeline.</p>

	<div class="poshcode code poshcode" style="font-family:monospace;"><br />
# Assignment<br />
$order = Get-ChildItem | Test-Pipeline<br />
$order | Format-Table *<br />
<br />
# Nested Pipelines<br />
Get-ChildItem | Where-Object { $_.PsIsContainer} | ForEach-Object {<br />
Get-ChildItem $_ | Test-Pipeline<br />
} | Select-Object Pipe*<br />
<br />
# Nested Expressions<br />
@( Get-ChildItem | Test-Pipeline )[0].PipelineLength | ForEach-Object { $_ }<br />
&nbsp;</div>

	<p>However, if you want to determine your function&#8217;s position in the pipeline for some other reason, the answer is simple. You need to use <code>$MyInvocation</code> and compare the <code>PipelineLength</code> and <code>PipelinePosition</code> properties:</p>

	<div class="poshcode code poshcode" style="font-family:monospace;"><br />
## Useful for testing all sorts of things about the pipeline<br />
function Test-Pipeline {<br />
[CmdletBinding()]<br />
Param(<br />
&nbsp; &nbsp;[Parameter(ValueFromPipeline=$true)]<br />
&nbsp; &nbsp;[PSObject]$InputObject,<br />
&nbsp; &nbsp;[Switch]$Passthru,<br />
&nbsp; &nbsp;[Switch]$PassCmdlet<br />
)<br />
BEGIN { <br />
&nbsp; &nbsp;Write-Output $MyInvocation<br />
&nbsp; &nbsp;if($PassCmdlet) {<br />
&nbsp; &nbsp; &nbsp; Write-Output $PsCmdlet<br />
&nbsp; &nbsp;}<br />
}<br />
PROCESS { if($Passthru){ $_ } }<br />
}<br />
<br />
## Shows <br />
function Test-LastInPipeline {<br />
Param(<br />
&nbsp; &nbsp;[Parameter(ValueFromPipeline=$true)]<br />
&nbsp; &nbsp;[PSObject]$InputObject,<br />
&nbsp; &nbsp;[Switch]$Passthru<br />
)<br />
BEGIN { <br />
&nbsp; &nbsp;$IsLast = $MyInvocation.PipelineLength -eq $MyInvocation.PipelinePosition<br />
&nbsp; &nbsp;if(!$IsLast) { $MyInvocation }<br />
}<br />
PROCESS { if($Passthru){ $_ } }<br />
}<br />
&nbsp;</div>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/d70821bd-4e06-4fc1-81f4-451e14c63c2f/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=d70821bd-4e06-4fc1-81f4-451e14c63c2f" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-determine-your-functions-position-in-the-pipeline/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Script Functions in the PowerShell Pipeline ( Take Two )</title>
		<link>http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline-take-two/</link>
		<comments>http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline-take-two/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 13:07:27 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Advanced Functions]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline-take-two/</guid>
		<description><![CDATA[One of the consistent questions about PowerShell is: what&#8217;s the best way to write a script or a function to process pipeline objects and be able to take it&#8217;s parameters as a normal function? Of scripts and functions The first thing to know is that in PowerShell, there&#8217;s really no difference between a script (just [...]]]></description>
			<content:encoded><![CDATA[	<p>One of the consistent questions about PowerShell is: what&#8217;s the best way to write a script or a  function to process pipeline objects <em>and</em> be able to take it&#8217;s parameters as a normal function?</p>

	<h3>Of scripts and functions</h3>

	<p class="em1">The first thing to know is that in PowerShell, there&#8217;s really no difference between a script (just a file with a .ps1 ending) and a function as you&#8217;ll see written below. If you take a function Get-Square, and remove the first and last lines (<code> Function Get-Square { </code> &#8230; <code> } </code>) you could put them in a file called &#8220;Get-Square.ps1&#8221; in your <span class="caps">PATH</span>, you use them exactly the way you would the function that was pre-loaded into memory by dot-sourcing or pasting it on the command line.  Of course, doing that easily requires writing the function parameters on their own line using <code>PARAM(...)</code> syntax, which is why I recommend doing that.</p>

	<h3>Of functions and the pipeline</h3>

	<p>When your script or function is used on the pipeline its <code>begin</code> block is called <em>once</em> when the pipeline starts up, and then the <code>process</code> block is called repeatedly: once to <em>process</em> each pipeline object, and finally, the <code>end</code> block is called after all the objects have been processed through the whole pipeline.  If you don&#8217;t understand that, you should play with this function, try calling it by passing a series of numbers through multiple instances of it:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquarePipe</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$label</span>, <span style="color: #660033; font-weight: bold;">$color</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;White&quot;</span> <span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Begin $Label&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;$Label <span style="color: #000099; font-weight: bold;">`t</span> $_&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">*</span> <span style="color: #660033; font-weight: bold;">$_</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">END</span> &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;End $Label&quot;</span> &nbsp; <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># for example ...</span><br />
1..5 <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquarePipe</span></span> one cyan <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquarePipe</span></span> two yellow <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquarePipe</span></span> three green<br />
&nbsp;</div>

	<p style="padding-left:2em;">Incidentally, if you don&#8217;t specify the <code>begin</code>, <code>process</code>, or <code>end</code> blocks, the body of your function is treated as the <code>end</code> block. This is so that you can use the special <code>$Input</code> variable, which collects all the things passed in on the pipeline, and thus only works in the <code>end</code> block.  That would allow the following function to behave the same way regardless of whether it was invoked on the pipeline or by passing an <code>$InputObject</code>. Notice, however, the difference between this, and the function above.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">Function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquareEnd</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$label</span>, <span style="color: #660033; font-weight: bold;">$color</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;White&quot;</span>, <span style="color: #660033; font-weight: bold;">$InputObject</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Begin $Label&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;$Label <span style="color: #000099; font-weight: bold;">`t</span> $_&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">END</span> &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## because one of $Input or $InputObject must be null:</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">Foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$item</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$Input</span> <span style="color: #66cc66;">+</span> <span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;$Label <span style="color: #000099; font-weight: bold;">`t</span> $item&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$item</span> <span style="color: #66cc66;">*</span> <span style="color: #660033; font-weight: bold;">$item</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># and test it like this</span><br />
1..5 <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquareEnd</span></span> one cyan <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquareEnd</span></span> two yellow <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquareEnd</span></span> three green<br />
<br />
<span style="color: #666666; font-style: italic;"># Or like this</span><br />
1..5 <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquarePipe</span></span> one cyan <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquareEnd</span></span> two yellow <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">SquareEnd</span></span> three green<br />
&nbsp;</div>

	<h3>Our challenge </h3>

	<p>The basic idea here is to rewrite that function such that it can be used to process a set of numbers from <em>either</em> the pipeline or an argument, without interfering with the processing of other parameters. We require that the function process items as they come in, rather than waiting until it&#8217;s received all input before processing them the way <code>Test-SquareEnd</code> does.</p>

	<h4>In PowerShell 2.0 this would be easy </h4>

	<p>In the current <span class="caps">CTP</span> 3 of PowerShell 2, you just specify <code>ValueFromPipeline=$true</code> for the parameter you want to set from the pipeline, and the function will work the same way whether you pass the numbers as a parameter or along the pipeline &#8212; you can even control which attribute of the objects on the pipeline will be used, but that&#8217;s <a href="a-guide-to-advanced-functions">a whole other article</a>.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">Function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$label</span><br />
, &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;White&quot;</span><br />
, &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$InputObject</span> <br />
<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Begin $Label&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">END</span> &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;End $Label&quot;</span> &nbsp; <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">ForEach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;$Label $i&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #66cc66;">*</span> <span style="color: #660033; font-weight: bold;">$i</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Either way we call these, they have the same output</span><br />
<span style="color: #666666; font-style: italic;">## Unlike what Get-SquarePipe or Get-SquareEnd </span><br />
<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> one green <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> two cyan<br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> one green <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span> &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> two cyan<br />
&nbsp;</div>

	<p>Of course, PowerShell 2.0 is still in beta status, and even after it&#8217;s released you may need to write scripts that are backwards compatible to PowerShell 1.0, and the excercise of doing so may  help you to understand more about how PowerShell functions work, and particularly how they behave in the pipeline.</p>

	<h3>Our solution</h3>

	<p>Since in a PowerShell 1.0 function it&#8217;s not really supported directly, we need to do some extra work:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$label</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;&quot;</span><br />
&nbsp; &nbsp;, &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ConsoleColor<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$color</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;White&quot;</span><br />
&nbsp; &nbsp;, &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$InputObject</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## If you accepted additional params, you'd need to pass those in</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$InputObject</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$MyInvocation</span>.<span style="color: #003366;">InvocationName</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-Label</span> <span style="color: #660033; font-weight: bold;">$label</span> <span style="color: #000066;">-Color</span> <span style="color: #660033; font-weight: bold;">$color</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## break</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Begin $Label&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## If you specify a type for $InputObject, test for that here</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;$Label $_&quot;</span> <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #66cc66;">*</span> <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;$_ is not a System.Int32&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">END</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;End $Label&quot;</span> &nbsp; <span style="color: #000066;">-Foreground</span> <span style="color: #660033; font-weight: bold;">$color</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
&nbsp;</div>

	<h3>An explanation</h3>

	<p>Of course, this is just an example method, squaring things isn&#8217;t that exciting &#8212; but what&#8217;s special about it is that the output is almost exactly the same whether you call it with parameters <code>Get-Square 1,2,3,4</code> or on the pipeline: <code>1,2,3,4 | Get-Square</code>.    </p>

	<p>The trick is that it actually executes the same way in either case:</p>

	<ol>
		<li>If you call it by passing the int (or array of ints) as an argument (<code>$InputObject</code>), it calls itself and passes those values on the pipeline.</li>
		<li>When the integers are passed on the pipeline, the special pipeline iterator variable <code>$_</code> is set, and the process block is executed.</li>
	</ol>
	<ol>
		<li>When it has to (re)invoke itself, it passes any other parameters as parameters, which means you need to have default values for them.</li>
	</ol>

	<p>There is <em>one</em> tiny difference in the processing, which in real-world use is practically never noticeable (you can see it in our example if you call it like this): </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> one green <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> two cyan<br />
<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> one green <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Square</span></span> two cyan<br />
&nbsp;</div>

	<p>You&#8217;ll see that unlike the PowerShell 2.0 pipeline function, when you pass the numbers as a parameter, the first one of them actually gets passed through the <code>process</code> block before the <code>begin</code> block of the second function on the pipeline is called. This usually doesn&#8217;t have any effect, but it&#8217;s something to keep in the back of your head.</p>

	<h3>A few precautions: </h3>

	<ul>
		<li>You have to default values for parameters, because you&#8217;ll be passing them all as named parameters, in the re-invoke step, and <code>$null</code> can cause problems.</li>
		<li>If you need to do additional processing in the <code>begin</code> block, you should only do so in an <code>ELSE</code> case: when $InputObject is null.  That way, the code will only execute <em>once</em> each time you call the function. </li>
		<li>The same goes for the <code>end</code> block: you have to keep your code in an <code>If(!$InputObject)</code> block to avoid executing it twice (when you pass the values as an argument, and it re-invokes itself). </li>
	</ul>
	<ul>
		<li>The test cases in the <code>process</code> block <em>must</em> wrap all of your process block code, so that you don&#8217;t process the arguments twice, and you shouldn&#8217;t refer to $InputObject, but instead should use the automatic <code>$_</code> variable which is the value passed when the function is (re)invoked via the pipeline.</li>
	</ul>

	<p>Here&#8217;s some sample output, in case you&#8217;re wondering:</p>

	<div class="text code text" style="font-family:monospace;"><br />
PS&gt; Get-Square test cyan 2,3,4<br />
Begin test<br />
test 2<br />
4<br />
test 3<br />
9<br />
test 4<br />
16<br />
End test<br />
<br />
PS&gt; 2,3,4|Get-Square test cyan<br />
Begin test<br />
test 2<br />
4<br />
test 3<br />
9<br />
test 4<br />
16<br />
End test<br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline-take-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PoshCode Updated for CTP3</title>
		<link>http://huddledmasses.org/poshcode-updated-for-ctp3/</link>
		<comments>http://huddledmasses.org/poshcode-updated-for-ctp3/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 20:35:17 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Advanced Functions]]></category>
		<category><![CDATA[CTP3]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PoshCode]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=930</guid>
		<description><![CDATA[I&#8217;ve updated the PoshCode script module to support CTP3, and added a -limit parameter to the Get-PoshCode cmdlet so you can specify how many items you want retrieved in the case where there are a lot of matches for your search terms &#8212; by default the limit is 25. Improvements to the underlying web search [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve updated the <a href="http://poshcode.org/PoshCode.psm1">PoshCode script module</a> to support CTP3, and added a -limit parameter to the Get-PoshCode cmdlet so you can specify how many items you want retrieved in the case where there are a lot of matches for your search terms &#8212; by default the limit is 25.</p>

	<h3>Improvements to the underlying web search <span class="caps">API</span></h3>

	<p>You’ve always been able to pass a <span class="caps">LIST</span> parameter to the <span class="caps">API</span>, and get more results by specifying a higher number. But it never worked with the &#8220;path&#8221; notation (until now).</p>

	<p>That is, you used to be able to do:</p>

	<ul>
		<li>http://poshcode.org/api1?q=start&#038;list=10</li>
	</ul>
	<ul>
		<li>http://poshcode.org/api1?q=start&#038;list=100</li>
	</ul>

	<p>To make the <span class="caps">API</span> a little easier to use I’ve enhanced it just now:</p>

	<ol>
		<li>You can now page the search results. </li>
		<li>You can use the word &#8220;limit&#8221; instead of &#8220;list&#8221;</li>
		<li>If you specify limit=0 (or list=0) I’ll give you everything I’ve got.  Please use a little precaution about that, as it could be a <span class="caps">LOT</span> of data. I’d much rather you retrieve, say … 25, and then get the second page if you want more.</li>
	</ol>
	<ol>
		<li>You can use path notation.</li>
	</ol>

	<p>So, you can use any of these URLs:</p>

	<ul>
		<li>http://poshcode.org/api1/start/list/25/page/1</li>
		<li>http://poshcode.org/api1/start/limit/25/page/2</li>
		<li>http://poshcode.org/api1?q=start&#038;list=25&#038;page=3</li>
	</ul>
	<ul>
		<li>http://poshcode.org/api1?q=start&#038;limit=25&#038;page=4</li>
	</ul>

	<p>There are a lot of search results for &#8220;start&#8221; ... feel free to play with enhancing the PoshCode module, or incorporating this into your apps, etc.</p>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  I should add that you don&#8217;t <em>have</em> to specify the limit or page number.  By default you&#8217;ll get the first 10 items, which should be enough.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/poshcode-updated-for-ctp3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pipeline Input vs Parameters in PowerShell scripts</title>
		<link>http://huddledmasses.org/pipeline-input-vs-parameters-in-powershell-scripts/</link>
		<comments>http://huddledmasses.org/pipeline-input-vs-parameters-in-powershell-scripts/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 06:20:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/pipeline-input-vs-parameters-in-powershell-scripts-2/</guid>
		<description><![CDATA[A relatively new PowerShell user came into #PowerShell on IRC.FreeNode.net this week to ask a question about scripts and the pipeline, and the conversation went so well, that I thought I&#8217;d share it with you all in case it helps clear things up for you. We&#8217;ll call him &#8220;user&#8221; since he left before I could [...]]]></description>
			<content:encoded><![CDATA[	<p>A relatively new PowerShell user came into #PowerShell on <span class="caps">IRC</span>.FreeNode.net this week to ask a question about scripts and the pipeline, and the conversation went so well, that I thought I&#8217;d share it with you all in case it helps clear things up for you.  We&#8217;ll call him &#8220;user&#8221; since he left before I could get his permission to paste this, and I&#8217;ve cleaned up, reordered and in a couple of cases added lines to make the conversation seem a little more linear than the chat really was.</p>

	<p><b style="width:5em; display: block; float: left;">user:</b> Can you explain the distinction between $_ and a param in a script context?<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> I sure can. And I think I can do it simply &#8230; take an example script|function &#8230; actually, we&#8217;ll just use a scriptblock<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> it can have pipeline blocks, or not (the pipeline blocks are the BEGIN{...} PROCESS{...} END{...})<br />
<b style="width:5em; display: block; float: left;">user:</b> yeah, I&#8217;m with you<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> so if you pass a value via an argument, it&#8217;s present in all three blocks, and has the same value the whole time (unless you change it in your script)<br />
<b style="width:5em; display: block; padding: 0px; float: left;">user:</b> <em>really</em><br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> But when you put that script in a pipeline, your <span class="caps">BEGIN</span> {} block is called first, and then for <span class="caps">EACH</span> item in the pipeline, powershell sets $_ and calls your <span class="caps">PROCESS</span> block, and finally, your END{} block is called after all pipeline items have been processed<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> Here, check this out (run it so you can see) <span id="more-518"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #009900;">&quot;one&quot;</span>,<span style="color: #009900;">&quot;two&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&amp;</span>amp;amp;<span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$Arg</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$Args</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Begin $Arg&quot;</span> <span style="color: #000066;">-Fore</span> Green <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Begin (empty) Pipeline: $_&quot;</span> <span style="color: #000066;">-Fore</span> Green<br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$Arg</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$Args</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Process $Arg&quot;</span> <span style="color: #000066;">-Fore</span> Cyan <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Process Pipeline: $_&quot;</span> <span style="color: #000066;">-Fore</span> Cyan<br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">END</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$Arg</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$Args</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;End $Arg&quot;</span> <span style="color: #000066;">-Fore</span> yellow <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;End Pipeline: $_&quot;</span> <span style="color: #000066;">-Fore</span> Yellow<br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #009900;">&quot;three&quot;</span>,<span style="color: #009900;">&quot;four&quot;</span> <span style="color: #009900;">&quot;five&quot;</span>,<span style="color: #009900;">&quot;six&quot;</span> <span style="color: #666666; font-style: italic;"># these are parameters to our script</span></div>

	<p><b style="width:5em; display: block; float: left;">user:</b> that really helps, thank you<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> no problem. Incidentally &#8230; in a script-block like that, you <em>can</em> use the Params() statement, but I wanted to loop over $args  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> <br />
<b style="width:5em; display: block; float: left;">user:</b> based on that I&#8217;m left wondering what the value of parameters is.<br />
<b style="width:5em; display: block; float: left;">user:</b> If I&#8217;m writing a script, chances are that I&#8217;m going to want to use it in a pipeline<br />
<b style="width:5em; display: block; float: left;">user:</b> I guess I can use parameters, but put it in a foreach-object block<br />
<b style="width:5em; display: block; float: left;">user:</b> $arr | %{myscript $_}<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> Well,  if you use the foreach-object block and pass arguments, your script doesn&#8217;t persist through the life of the pipeline, so you can&#8217;t do things like counting, or comparing to the previous object, etc.<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> But you can only pass one thing at a time via the pipeline, so you still need arguments in a script like the one I just used to paste that script to the pastebin (which is here: http://huddledmasses.org/powershell-send-paste-script/ )<br />
<b style="width:5em; display: block; float: left;">user:</b> right. So I guess the concept is that scripts grab stuff off the pipeline themselves?<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> sort-of, yeah. I mean, it&#8217;s actually PowerShell that does it, but basically, the process block grabs each item off the pipeline without needing it to be specified as an argument.</p>

	<p><b style="width:5em; display: block; float: left;">Jaykul:</b> In v2, you can be more explicit about this, because you can write a &#8220;cmdlet&#8221; in script which lets you specify parameters which should get the pipeline value<br />
<b style="width:5em; display: block; float: left;">user:</b> okay, I totally get it now. That&#8217;s why you&#8217;d write a cmdlet instead of a script?<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> Yeah! The particularly cool thing about cmdlet parameter handling &#8230; is you can do this:<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> [Parameter(FromPipelineByPropertyValue)] [Alias(&#8220;FullName&#8221;)] $FileName<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> a parameter like that would accept a string as a parameter, but if you did <code>gci * | scriptcmdlet</code> instead of getting the [System.IO.FileInfo] object you would get the <span class="caps">STRING</span> corresponding to that object&#8217;s FullName property<br />
<b style="width:5em; display: block; float: left;">user:</b> right. sweet!<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> so you can even do tricky stuff like getting multiple properties from it (as different parameters to your script)</p>

	<p><b style="width:5em; display: block; float: left;">user:</b> I noticed from http://powershellcentral.com/scripts/184 that you&#8217;re testing the type of the pipeline object<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> yeah<br />
<b style="width:5em; display: block; float: left;">user:</b> FIleInfo vs. string<br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> because it lets me do this: <code>ls *.ps1 | Send-Paste</code><br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> or this: <code>get-history -count 5 | % { $_.CommandLine } | Send-Paste</code><br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> or even send multiple files as a single paste, like: <code>get-content *.ps1 | Send-Paste</code><br />
<b style="width:5em; display: block; float: left;">Jaykul:</b> (note that in this script, that&#8217;s <span class="caps">VERY</span> different than ls *.ps1 | Send-Paste &#8230;. which would create a paste for each file)<br />
<b style="width:5em; display: block; float: left;">user:</b> wow, this is good stuff, thanks for the time&#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/pipeline-input-vs-parameters-in-powershell-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Better Script Functions for the PowerShell Pipeline</title>
		<link>http://huddledmasses.org/writing-better-script-functions-for-the-powershell-pipeline/</link>
		<comments>http://huddledmasses.org/writing-better-script-functions-for-the-powershell-pipeline/#comments</comments>
		<pubDate>Fri, 26 Oct 2007 04:41:59 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/writing-better-script-functions-for-the-powershell-pipeline/</guid>
		<description><![CDATA[I wrote a post last week about how to write functions for use in the PowerShell pipeline and I&#8217;ve been using the template I wrote in that post as the basis for several of my other scripts &#8230; and I&#8217;ve been gradually fleshing it out, and improving it, so I thought I&#8217;d drop it here [...]]]></description>
			<content:encoded><![CDATA[	<p>I wrote a post last week about how to <a href="/using-script-functions-in-the-powershell-pipeline/">write functions for use in the PowerShell pipeline</a> and I&#8217;ve been using the template I wrote in that post as the basis for several of my other scripts &#8230; and I&#8217;ve been gradually fleshing it out, and improving it, so I thought I&#8217;d drop it here with all of it&#8217;s inline comments.  Hopefully that will be better than what you would get if I just trying to explain it in a blog post  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> .</p>

	<p>Incidentally, this script is also on <a href="http://powershellcentral.com/scripts/36">PowerShellCentral scripts Repository</a> where I will probably post any future modifications &#8230; <span id="more-454"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## A Template for functions which can _also_ be executed in the pipeline ....</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; by Joel Bennett, in hopes it will help...</span><br />
<span style="color: #666666; font-style: italic;">## Version History</span><br />
<span style="color: #666666; font-style: italic;">## v1.0 First public release (after over 9 different versions in my various other functions)</span><br />
<span style="color: #666666; font-style: italic;">## v1.2 Show the use of Write-Output, and change &quot;return&quot; in the BEGIN to &quot;Write-Output&quot; to avoid</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp;the pooling of the output from the process block when it's invoked as a function.</span><br />
<span style="color: #666666; font-style: italic;">## v1.3 Switched back to &quot;break&quot; instead of &quot;return&quot; so that if you pass via the pipeline AND via</span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp;the inputObject, only the inputObject gets process (this is how cmdlets behave).</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## The simplest thing is to make -inputObject the very last argument and make sure that you name </span><br />
<span style="color: #666666; font-style: italic;">## each parameter that you expect to see ... as always you'll have to specify a value for each </span><br />
<span style="color: #666666; font-style: italic;">## parameter, or specifically name the inputObject parameter.</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Pipeline</span></span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Parameter1</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$null</span>, <span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$null</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">### If you're accepting $args, you need to pass those in...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Write-Output $io | &amp;($MyInvocation.InvocationName) $args;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$inputObject</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$MyInvocation</span>.<span style="color: #003366;">InvocationName</span><span style="color: #333;">&#41;</span> <span style="color: #660033; font-weight: bold;">$Parameter1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">break</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">else</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> &nbsp;<span style="color: #666666; font-style: italic;">## DO ONCE: (on the re-invoke when using -inputObject)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">host</span></span> <span style="color: #009900;">&quot;Begin $Parameter1 $args&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## You have to at least make sure it's got a value </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Really you should check it's TYPE to make sure you can do something useful...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Write-HOST: $_&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## You should make a practice of explicitly calling Write-Output on things</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## That's how you emit them into the pipeline instead of just printing them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #009900;">&quot;Write-OUTPUT $_&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Most of the time, piping to Out-Default is a lot like using Write-Host...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Out-Default $_&quot;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">Default</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">END</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #333399; font-weight: bold; font-style: italic;">not</span> <span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> &nbsp;<span style="color: #666666; font-style: italic;">## DO ONCE: (on the re-invoke when using -inputObject)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$args</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$x</span><span style="color: #66cc66;">++</span>; <span style="color: #009900;">&quot;$x - $_&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">host</span></span> <span style="color: #009900;">&quot;End&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Sample call:</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## [1]&gt; Test-Pipeline Foo! @(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;)</span><br />
<span style="color: #666666; font-style: italic;">## Begin Foo!</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: one</span><br />
<span style="color: #666666; font-style: italic;">## Write-OUTPUT one</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default one</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: two</span><br />
<span style="color: #666666; font-style: italic;">## Write-OUTPUT two</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default two</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: three</span><br />
<span style="color: #666666; font-style: italic;">## Write-OUTPUT three</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default three</span><br />
<span style="color: #666666; font-style: italic;">## End</span><br />
<br />
<span style="color: #666666; font-style: italic;">## [2]&gt; &quot;one&quot;, &quot;two&quot;, &quot;three&quot; | Test-Pipeline Foo!</span><br />
<span style="color: #666666; font-style: italic;">## Begin Foo!</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: one</span><br />
<span style="color: #666666; font-style: italic;">## Write-OUTPUT one</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default one</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: two</span><br />
<span style="color: #666666; font-style: italic;">## Write-OUTPUT two</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default two</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: three</span><br />
<span style="color: #666666; font-style: italic;">## Write-OUTPUT three</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default three</span><br />
<span style="color: #666666; font-style: italic;">## End</span><br />
<br />
<span style="color: #666666; font-style: italic;">## ## ## To demonstrate the point of output, assign the value, or pipe it into something...</span><br />
<span style="color: #666666; font-style: italic;">## [3]&gt; $pipeline = &quot;one&quot;, &quot;two&quot;, &quot;three&quot; | Test-Pipeline Foo!</span><br />
<span style="color: #666666; font-style: italic;">## Begin Foo!</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: one</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default one</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: two</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default two</span><br />
<span style="color: #666666; font-style: italic;">## Write-HOST: three</span><br />
<span style="color: #666666; font-style: italic;">## Out-Default three</span><br />
<span style="color: #666666; font-style: italic;">## End</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/writing-better-script-functions-for-the-powershell-pipeline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Script Functions in the PowerShell Pipeline</title>
		<link>http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline/</link>
		<comments>http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 20:09:47 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/using-script-functions-in-the-powershell-pipeline/</guid>
		<description><![CDATA[Update, I created a better version of a pipeline function for powershell &#8230; Every once in a while the question of how to best use the process block of a function to process pipeline objects comes up on IRC, and although I&#8217;m sure others have already written this up on the web in the past, [...]]]></description>
			<content:encoded><![CDATA[	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  Update, I created <a href="/writing-better-script-functions-for-the-powershell-pipeline/">a better version of a pipeline function for powershell</a> &#8230;</p>

	<p>Every once in a while the question of how to best use the <strong>process</strong> block of a function to process pipeline objects comes up on <span class="caps">IRC</span>, and although I&#8217;m sure others have already written this up on the web in the past, we&#8217;ve been polishing up this example sending it back and forth to each other for a couple of weeks, and it seems to me it&#8217;s about time to publish it a little more prominently.</p>

	<p>The basic idea was to write a function that could be used to process a set of inputs from either the pipeline or an argument, while still allowing other arguments to be passed and processed.  This is something that&#8217;s very easy for a cmdlet, because you can specify that a certain parameter will receive pipeline input (and even control which attribute of the objects on the pipeline will be used), but in a script it&#8217;s not really supported.  In addition, we require that the function process items as they come in, rather than waiting until it&#8217;s received all input before processing them.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Pipeline</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$inputObject</span>,<span style="color: #660033; font-weight: bold;">$io</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$inputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$io</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$MyInvocation</span>.<span style="color: #003366;">InvocationName</span><span style="color: #333;">&#41;</span> <span style="color: #660033; font-weight: bold;">$args</span>; <span style="color: #666699; font-weight: bold;">break</span>;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$args</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$io</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #660033; font-weight: bold;">$args</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">host</span></span> <span style="color: #009900;">&quot;Begin $args&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">host</span></span> <span style="color: #009900;">&quot;PROCESSME: $_&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">END</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$x</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$args</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$x</span><span style="color: #66cc66;">++</span>; <span style="color: #009900;">&quot;$x - $_&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">host</span></span> <span style="color: #009900;">&quot;End&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h4>Some explanation</h4>

	<p>This is just an example method, it doesn&#8217;t really do much, except that it outputs exactly the same thing whether you call it like <code>Test-Pipeline -inputObject &#34;Foo&#34;,&#34;Bar&#34;,&#34;Baz&#34; &#34;Kill Each Process&#34;</code> or like: <code>&#34;&#34;Foo&#34;,&#34;Bar&#34;,&#34;Baz&#34; | Test-Pipeline &#34;Kill Each Process&#34;</code>.    </p>

	<p>It works by calling itself with the inputObject argument passed on the pipeline when you specify it as an argument, so that code is processed exactly right.  This means that the first line you see in the <code>process</code> block <em>must</em> remain the first line, even if you add additional code to the block.  </p>

	<p>We use a trick to make sure that additional unnamed parameters work normally by using a switch parameter -inputObject followed by a second parameter $io which we treat as inputObject if inputObject is set.  This works because switch parameters don&#8217;t expect values, so we&#8217;re able to sort-of hijack it to emulate the cmdlet behavior. Note that if it is not set, we move the value of the $in parameter into the $args array which contains any additional parameters.  This arrangement allows it to <em>behave</em> rather like a cmdlet would, but it&#8217;s a little delicate: if you&#8217;re going to pass input objects, you <strong>must</strong> specify the switch as the first argument, and pass the $io (inputObjects) immediately following (as though they were the value of the inputObjects parameter). All of that is necessary so that we can optionally pass additional parameters.  </p>

	<p>You can also add additional named parameters &#8212; but you would need to either always specify their names when you called them, or carefully adjust the code so that they get shifted if -inputObject isn&#8217;t specified.</p>

	<p>Here&#8217;s some sample output, in case you&#8217;re wondering:</p>

	<div class="text code text" style="font-family:monospace;"><br />
PS&gt; $foo = &quot;Greetings&quot;,&quot;Earthling&quot;<br />
PS&gt; $foo | Template-Pipeline &quot;Hello&quot; &quot;World&quot;<br />
Begin Hello World<br />
PROCESSME: Greetings<br />
PROCESSME: Earthling<br />
1 - Hello<br />
2 - World<br />
End<br />
PS&gt; Template-Pipeline -in $foo &quot;Hello&quot; &quot;World&quot;<br />
Begin Hello World<br />
PROCESSME: Greetings<br />
PROCESSME: Earthling<br />
1 - Hello<br />
2 - World<br />
End<br />
&nbsp;</div>

	<h4>Credit where it&#8217;s due</h4>

	<p>I wanted to officially acknowledge that <a href="http://thepowershellguy.com/blogs/posh/">/\/\o\/\/</a> and <a href="http://thepowershellguy.com/blogs/gaurhoth/">Gaurhoth</a> and <a href="http://www.nivot.org/">Oisin</a> and <a href="http://bsonposh.com/">Brandon</a> have all contributed to the polishing and testing of this script in various forms &#8230; thanks to all  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>About_Filter considered Harmful</title>
		<link>http://huddledmasses.org/about_filter-considered-harmful/</link>
		<comments>http://huddledmasses.org/about_filter-considered-harmful/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 17:22:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Functions]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/about_filter-considered-harmful/</guid>
		<description><![CDATA[Someone asked a question in the #PowerShell channel on irc.FreeNode.net today about how to use filters, and pasted this an example like this: filter process-a-m &#123; &#160; &#160;$_.processname -like &#34;[a-m]*&#34; &#125; Get-Process &#124; where &#123;process-a-m&#125; The question was: why doesn&#8217;t this have any output? Well, the answer is: it can&#8217;t have any output. The filter [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked a question in the #PowerShell channel on irc.FreeNode.net today about how to use filters, and pasted this an example like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">filter</span> process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m<br />
<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[a-m]*&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Process</span></span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span>process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m<span style="color: #333;">&#125;</span></div>

	<p>The question was: why doesn&#8217;t this have any output? Well, the answer is: it can&#8217;t have any output. The <em>filter</em> outputs true or false for each item passed into it, but the <em>where</em> scriptblock doesn&#8217;t actually pass anything into the filter! Of course we quickly fixed his problem <em>the right way</em> by rewriting the filter to output the items, and getting rid of the where-object call completely &#8230; but when I started wondering where he had gotten the bizarre idea to use a filter like that, his answer was: &#8220;I read about_filter.&#8221;  So I went and looked, and sure enough, the example pasted above is straight out of about_filter.  <strong>And it&#8217;s as wrong as it can possibly be</strong>.</p>

	<p>Let me fix it, and then offer some clarification below.  In <strong>about_filter</strong> there is an example like this: <code>Get-Process | where {$_.processname -like &#34;[a-m]*&#34;}</code> and then a table describing each element of the Where-Object command, and just below it, there is some text about the <strong><em>filter</em></strong> command.  Mentally replace that text with this:<span id="more-445"></span> </p>

	<blockquote>
		<p>You can create a predefined filter by creating a special type of function. When defining the function, you may specify that it is a filter. For the filter script block, you should output items if they pass the test you saw in the preceding example. For instance, the following command creates a filter named process-a-m:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">filter</span> process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m<br />
&nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[a-m]*&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span></div>
		<p>You can then use the filter name in place of your Where-Object command to retrieve the filtered data, as shown in the following example:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Process</span></span> <span style="color: #66cc66;">|</span> process<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m</div>
		<p>The values returned by the command are the same as they would have been had you specified the conditions using the Where-Object cmdlet as above, but the filter is much easier to reuse on multiple calls, particularly if the conditions were more complicated.</p>
		<p>Of course, a filter function can take parameters, just like any other function, so you could make the range of starting letters more flexible:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">filter</span> process<span style="color: #66cc66;">-</span>range<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">char</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$a</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;a&quot;</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">char</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$m</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;m&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[$a-$m]*&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span></div>
		<p>Now you can use this filter to select between any two letters quite easily:</p>
		<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Process</span></span> <span style="color: #66cc66;">|</span> <span style="color: #666699; font-weight: bold;">process</span> b j</div>
	</blockquote>

	<h4>Functions vs. Filters</h4>

	<p>To be clear, functions in PowerShell aren&#8217;t like functions in other programming languages, although they <strong>can</strong> just have a body (and be like other functions) they can instead have pipeline syntax.  A function (just like a cmdlet) which is meant to be used in the pipeline has three separate bodies: <strong>begin</strong>, <strong>process</strong>, and <strong>end</strong>, but a filter has only the <em>process</em> part of the body.</p>

	<p>Of course, there are other uses for filters than just limiting which objects come through &#8230; anything that you could do with foreach-object or where-object, you can basically do with a filter in a much more reusable way.  So you don&#8217;t have to write the same thing to the output as what came in the input, you could actually add to it, or remove from it.  A filter for processes could look use Win32_Process to look up their &#8216;owner&#8217; and append it as a property, or could return the process path and name as text &#8230;</p>

	<p>If you&#8217;re new to all of this, let me just present an example function to hopefully clear things up:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">Function</span> DemoTheCount<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$things</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;things&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">begin</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Let us count $things!&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$counter</span><span style="color: #66cc66;">++</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #009900;">&quot;$counter wonderful $things! &nbsp; &nbsp;$_&quot;</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">end</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Ha, ha, ha, ha .. $counter wonderful $($_.GetType())es!&quot;</span> <br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>A function without an internal pipeline block is basically like the <strong>begin</strong> block of a pipeline-enabled function: the only things present are the parameters to the function (if there are any).  Then, <strong>process</strong> is called once for each item in the pipeline, with the current item set as the $_ variable. Finally, <strong>end</strong> is called, and you still have the last item in the pipeline present.  The scope persists through each part of the function (so you will see that $counter maintains it&#8217;s value).  The important distinction between this and a function without a <strong>process</strong> body is that a normal function will only be called <em>once</em> when it is in a pipeline &#8212; regardless of how many things are in the pipeline.  If you called DemoTheCount with a list of processes, like <code>Get-Process g* | DemoTheCount</code>, you would get something like this (depending on the processes running on your PC):</p>

	<div class="txt code txt" style="font-family:monospace;">Let us count!<br />
1 wonderful things! &nbsp; &nbsp;System.Diagnostics.Process (gajim)<br />
2 wonderful things! &nbsp; &nbsp;System.Diagnostics.Process (GeoShell)<br />
Ha, ha, ha, ha .. 2 wonderful System.Diagnostics.Process(es)!</div>

	<p>A filter doesn&#8217;t have separate parts, because the <strong>whole</strong> filter is the <strong><em>process</em></strong> part of the function.  If we were to take the process portion of our <code>DemoTheCount</code> function and turn it into a filter, it would only work because the dynamic language would automatically initialize $counter to zero and let you increment it &#8212; anything more complex (or where the type couldn&#8217;t be deduced) would not. When you create a filter using the filter keyword, it&#8217;s pure syntax sugar.  The filter <code>process-a-m</code> defined in my correction to the documentation is <strong>exactly</strong> the same as this function:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> reprocess<span style="color: #66cc66;">-</span>a<span style="color: #66cc66;">-</span>m <br />
<span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">processname</span> <span style="color: #000066;">-like</span> <span style="color: #009900;">&quot;[a-m]*&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span></div>

	<p>You can verify this by creating both of them and then comparing the contents of the resulting functions (<code>(get-content function:\process-a-m) -eq (get-content function:\reprocess-a-m)</code>.  The filter keyword simply creates a function and with the filter body as the <strong>process</strong> body for the function.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/about_filter-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

