<?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; Advanced Functions</title>
	<atom:link href="http://joelbennett.net/tag/advanced-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Tue, 31 Aug 2010 04:13:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Using Script Functions in the PowerShell Pipeline ( Take Two )</title>
		<link>http://joelbennett.net/using-script-functions-in-the-powershell-pipeline-take-two/</link>
		<comments>http://joelbennett.net/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://joelbennett.net/using-script-functions-in-the-powershell-pipeline-take-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A guide to PowerShell&#8217;s Advanced Functions</title>
		<link>http://joelbennett.net/a-guide-to-advanced-functions/</link>
		<comments>http://joelbennett.net/a-guide-to-advanced-functions/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 03:44:03 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Advanced Functions]]></category>
		<category><![CDATA[CTP3]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell 2]]></category>
		<category><![CDATA[PowerShell Functions]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1116</guid>
		<description><![CDATA[Someone asked on the PowerShell Newsgroup about writing Advanced Functions, and specifically: looking for a &#8230; guide to putting together an advanced function that is visible and usable every time I start Powershell. By visible I mean that when I do a &#8216;get-command&#8217; I want my [advanced function]s to be listed alongside all the regular [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked on the <a href="news://msnews.microsoft.com:119/microsoft.public.windows.powershell">PowerShell Newsgroup</a> about writing Advanced Functions, and specifically:</p>

	<blockquote>
		<p>looking for a &#8230; guide to putting together an advanced function that is visible and usable every time I start Powershell. By visible I mean that when I do a &#8216;get-command&#8217; I want my [advanced function]s to be listed alongside all the regular cmdlets. What makes that possible? ... what do I need to do to make that happen? Whats the difference between an [advanced function] and a module?</p>
	</blockquote>

	<p>There are lots of articles on the Microsoft <a href="http://blogs.msdn.com/powershell/">PowerShell team blog</a> about both topics, but it seems there&#8217;s not really been any sort of step-by-step written, so I posted this to the newsgroup, and since the person who asked the original question said he found it useful, I figured I&#8217;d share it here&#8230;</p>

<span id="more-1116"></span>

	<h2>An Aside: Advanced Functions vs. Modules</h2>

	<p>Advanced functions and script modules are unrelated topics. A script module could have nothing but old PowerShell 1 -type functions, hypothetically, it could even not have any functions at all.  Advanced functions can be defined in a plain .ps1 script, in your profile, or by typing them in on the command-line, etc. The two things are essentially unrelated except for both being new features in PowerShell 2.0.</p>

	<h2>How To write an Advanced Function</h2>

	<h3>Step 1: Have something you want to do in a function.</h3>

	<p>I can&#8217;t emphasize enough how important it is that you should have a specific goal here. Writing a script for a specific purpose is much easier, because you don&#8217;t have to think about whether or not specific features are necessary, etc.</p>

	<p>For the purposes of this example, I want a function to start executables and return me the process object (so I can wait for it to finish, or whatever). I will call it Start-Process.</p>

	<h3>Step 2: Write a first step for the logic of your function. </h3>

	<p>Be sure to specify the parameters using the <code>functionName{Param(...)}</code> syntax rather than <code>functionName(...){}</code>, then you will convert it to an advanced function by adding <code>[CmdletBinding()]</code> on a line right before the <code>Param</code>:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$app</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$param</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$app</span>, <span style="color: #660033; font-weight: bold;">$param</span> <span style="color: #333;">&#41;</span><br />
&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; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Step 3: Start writing documentation and auto-help, including your parameters.</h3>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Starts an application, with optional command-line parameters.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter App</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The path to the application you want to start</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Params</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string consisting of all the parameters to pass to App</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$app</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$param</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$app</span>, <span style="color: #660033; font-weight: bold;">$param</span> <span style="color: #333;">&#41;</span><br />
&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; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Step 4: Exploit advanced features by marking up your parameters.</h3>

	<p>In my case, I had a look at the signature of the start method by running <code>[Diagnostics.Process]::Start.OverloadDefinitions</code> and discovered that it can take credentials too.  So I wanted an <em>optional</em> credential parameter. I specified it without a <strong>position</strong>, and made it part of a non-default parameter set.</p>

	<p>I also wanted the <code>params</code> parameter to just take any additional parameters which I pass to the function, and pass them on to the process that I&#8217;m starting. </p>

	<p>Finally, I wanted the &#8220;App&#8221; parameter to be able to come from the pipeline. Specifically, I wanted to be able to pass the output of Get-Command or Get-ChildItem to it. In order to enable that, I defined an alias on it to the relevant properties of the objects which come from those two commands</p>

	<p>Hopefully you&#8217;re getting the idea of what&#8217;s possible here &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #666666; font-style: italic;">#.Synopsis</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Starts an application, with optional command-line parameters.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter App</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The path to the application you want to start</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Params</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The string consisting of all the parameters to pass to App</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Credential</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;PSCredential containing valid login to &quot;run as&quot; another user.</span><br />
<span style="color: #666666; font-style: italic;">################################################################</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span>DefaultParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;NoCreds&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ValueFromPipelineByPropertyName<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; <span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;FullName&quot;</span>,<span style="color: #009900;">&quot;Path&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$app</span><br />
,<br />
&nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;RunAs&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;PSCredential&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSCredential</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Credential</span><br />
,<br />
&nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">3</span>, Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ValueFromRemainingArguments<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #333;">&#41;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</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;">$params</span><br />
<span style="color: #333;">&#41;</span><br />
<br />
&nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$credential</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cred</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">GetNetworkCredential</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<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;">string</span><span style="color: #333;">&#93;</span></span>::<span style="color: #333399; font-weight: bold; font-style: italic;">join</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &quot;</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">UserName</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">Password</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">Domain</span> <span style="color: #333;">&#41;</span><br />
&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; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">UserName</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$credential</span>.<span style="color: #003366;">Password</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$cred</span>.<span style="color: #003366;">Domain</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&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; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$app</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<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;">string</span><span style="color: #333;">&#93;</span></span>::<span style="color: #333399; font-weight: bold; font-style: italic;">join</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &quot;</span>,<span style="color: #660033; font-weight: bold;">$params</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span><br />
&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; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$app</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<h3>Bonus Step: Make the function be automatically defined</h3>

	<p>To be honest, there are a lot of ways to do this, here&#8217;s a few:</p>

	<h4>Option 1. Define it in your Profile.</h4>

	<p>Include the full text of the function in your Profile.ps1 script or Microsoft.PowerShell_profile.ps1 &#8230; just run <code>notepad $Profile</code> and paste the whole function in at the top, then save.</p>

	<h4>Option 2. Dot-source it in your Profile.</h4>

	<p>Save the function into a script file, e.g.: &#8220;StartProcessFunction.ps1&#8221; in your DocumentsWindowsPowerShell folder (next to your profile script) then put the line: <code>. StartProcessFunction.ps1</code> into your profile script to dot-source it. Using the dot causes the script to be loaded into the context as though you had put the contents of the script in, instead of the dot-source line. </p>

	<h4>Option 3. Import this function from a script file.  </h4>

	<p>This is basically the same as Option 2, except instead of &#8220;.&#8221; you can use <code>Import-Module</code>.  The Import-Module cmdlet, when used on a file with the .ps1 extension, is basically the same as dot-sourcing it, so there&#8217;s not a whole lot of benefit over dot-sourcing in this case.</p>

	<h4>Option 4. Put the function in a module.</h4>

	<p>You should <strong>only</strong> do this if you think <em>you might want to add additional functions</em> to the module. For instance, lets say you want to write a few other functions for working with process objects.  If you put them together in the same module (say: &#8220;ProcessUtility&#8221;) they can share script-scoped variables or private functions, and can be identified as being part of the same module using the <code>Get-Command -Module ProcessUtility</code> command. </p>

	<p>If you just have the single function, there&#8217;s really no point in changing it to a Module, but I&#8217;m not aware of any downside, either.</p>

	<h4>Option 4a. Make a module by just saving as a <code>.psm1</code></h4>

	<p>All you do is save the function into a module file, e.g.: &#8220;ProcessUtility.psm1&#8221; in your DocumentsWindowsPowerShell folder and then put the line: <code>Import-Module ProcessUtility.psm1</code> into your profile script.</p>

	<h4>Option 4b. Make a module folder.</h4>

	<p>This is by far the most extensible way to create modules. You paste the function into &#8220;ProcessUtility.psm1&#8221; and then save it into a folder &#8220;ProcessUtility&#8221; which must be created in one of your module folders. You can figure out where the module folders are by checking <code>$Env:PSMODULEPATH</code> &#8212; the normal spot is your DocumentsWindowsPowerShellModules. </p>

	<p>Once that&#8217;s done, you can import with the simpler command <code>Import-Module ProcessUtility</code> without any extension. </p>

	<p>The nice thing about creating the folder is that you can put any additional resources in that folder with it, and get at them using the <code>$PsScriptRoot</code> to define the folder. You can even add additional functions by putting them in separate files (which you can Import-Module from the main file, or you can define using a .psd1, but that&#8217;s <a href="http://huddledmasses.org/powershell-modules-metadata-and-mysteries/">another article</a>).</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/a-guide-to-advanced-functions/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PoshCode Updated for CTP3</title>
		<link>http://joelbennett.net/poshcode-updated-for-ctp3/</link>
		<comments>http://joelbennett.net/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://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/new.gif' 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://joelbennett.net/wordpress/wp-includes/images/smilies/../../../wp-content/plugins/smilingmasses/smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/poshcode-updated-for-ctp3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
