<?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; Alpha</title>
	<atom:link href="http://huddledmasses.org/tag/alpha/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerShell 2.0 CTP &#8211; Script Cmdlets</title>
		<link>http://huddledmasses.org/powershell-20-ctp-script-cmdlets/</link>
		<comments>http://huddledmasses.org/powershell-20-ctp-script-cmdlets/#comments</comments>
		<pubDate>Tue, 06 Nov 2007 22:09:57 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Alpha]]></category>
		<category><![CDATA[Beta]]></category>
		<category><![CDATA[Cmdlet]]></category>
		<category><![CDATA[CTP]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Pipeline]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-20-ctp-script-cmdlets/</guid>
		<description><![CDATA[Well, the first alpha CTP release of PowerShell 2.0 is out, and there&#8217;s a lot of new stuff in it &#8230; but I won&#8217;t repeat the list from the PowerShell blog, because I&#8217;m sure you&#8217;ve seen it five or six times already. Instead, lets just skip straight to talking about one of the features we&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, the first <a href="http://blogs.msdn.com/powershell/archive/2007/11/06/what-s-new-in-ctp-of-powershell-2-0.aspx"><del>alpha</del> <span class="caps">CTP</span> release of PowerShell 2.0</a> is out, and there&#8217;s a lot of new stuff in it &#8230; but I won&#8217;t repeat the list from the PowerShell blog, because I&#8217;m sure you&#8217;ve seen it five or six times already. Instead, lets just skip straight to talking about one of the features we&#8217;ve been hearing about the longest: in PowerShell 2, you can create Cmdlets in script &#8230; bringing nearly full parity between  whats possible in a C# cmdlet and what&#8217;s possible in script.</p>

	<p>There are a few caveats still (Parameter Sets aren&#8217;t working yet, and neither is help, really), and a few surprises &#8230; there&#8217;s a few downsides to PowerShell script vs C# ... but in this particular context one thing that stands out is that in C# the <code>BeginProcessing</code>, <code>ProcessRecord</code>, and <code>EndProcessing</code> blocks are actually methods which can call each other, and as demonstrated in my <a href="/writing-cmdlets-for-the-powershell-pipeline/">tutorial for writing cmdlets that work in the pipeline</a>, they can be recursive &#8212; without getting duplicate variables.  </p>

	<h4>A sample Script Cmdlet</h4>

	<p>In the interests of being the first to publish an interesting script cmdlet  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  and to continue my recent trend of talking about writing for the PowerShell pipeline, I&#8217;ve merged the logic of my <a href="/writing-better-script-functions-for-the-powershell-pipeline/">script function</a> and my <a href="/writing-cmdlets-for-the-powershell-pipeline/">pipeline cmdlet</a> into a <a href="/wordpress/wp-content/uploads/2007/11/test-pipelinev2.ps1">single sample script cmdlet for PowerShell 2.0</a> and it works great!</p>

	<p>A few observations from the process, in no particular order:</p>

	<ul>
		<li>If you recursively call your cmdlet from within itself, you have to test for parameters using the new <code>$CommandLineParameters.ContainsKey</code> because parameter variables keep their values through recursion if you don&#8217;t explicitly pass a value.</li>
		<li><code>$CommandLineParameters.ContainsKey</code> works differently in the <code>Begin</code> block where it will return <code>$false</code> for arguments which will get their values from the pipeline, than in the <code>Process</code> block where it will treat values which were passed as <code>CommandLineParameters</code> the same as those which were passed via the pipeline.</li>
		<li>If you want to see how your function behaves in a pipeline, you should make sure to test it at different points in the pipeline: at the front, in the middle, and at the end.</li>
		<li>Cmdlets are functions: they show up in the Function provider.</li>
		<li>Cmdlets are functions: they have to be dot-sourced before you can call them.</li>
		<li>Cmdlets are not functions: they are a single command <code>Cmdlet</code> which takes a name (which <strong>must</strong> have a &#8211; in it) and a couple of other parameters followed by a function script block.</li>
	</ul>
	<ul>
		<li>When you recurse by executing &#038;($MyInvocation.InvocationName), that second invocation has an InvocationName of &#8220;&#038;&#8221; ... so you can&#8217;t go any further (this might be a good thing, if you want to stop recursion at one level no matter what. If you want to go further, you need to put your commands into a string, and use <code>Invoke-Expression</code>.</li>
	</ul>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">#requires -version 2.0</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## A Template for Script Cmdlets 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;">## &nbsp; &nbsp; &nbsp;- Cleaned up the comments, and removed the confusing alternate method and $args handling</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## v2.0 - First Version as a Script Cmdlet. </span><br />
<span style="color: #666666; font-style: italic;">## &nbsp; &nbsp; &nbsp;This is much easier with support for [ValueFromPipeline] and [ValueFromPipelineByName] </span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
Cmdlet <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">PipelineV2</span></span> <span style="color: #000066;">-ConfirmImpact</span> low <span style="color: #000066;">-snapin</span> Huddled.<span style="color: #003366;">Tests</span><br />
<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: #333;">&#91;</span>Position<span style="color: #333;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span> <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>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Position<span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Mandatory<span style="color: #333;">&#93;</span></span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>ValueFromPipeline<span style="color: #333;">&#93;</span></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;">&#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 />
&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;">$CommandLineParameters</span>.<span style="color: #003366;">ContainsKey</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;InputObject&quot;</span><span style="color: #333;">&#41;</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;"># Don't do anything here, because we're about to get re-invoked...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$FromArgs</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</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: #666666; font-style: italic;"># Normal &quot;run-once&quot; BEGIN processing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$FromArgs</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Begin $Color&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;"># We no longer have to test for $_ or even to see if the [ValueFromPipeline] param is set</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># It *HAS* to be set, because it's a [Mandatory] parameter :)</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;">$FromArgs</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;"># Don't do anything here except re-invoke ourselves.</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;">$Color</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: #666666; font-style: italic;"># Normal Pipeline-friendly per-item processing</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;Process: $InputObject&quot;</span> <span style="color: #000066;">-Fore</span> <span style="color: #660033; font-weight: bold;">$Color</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: #660033; font-weight: bold;">$InputObject</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: #660033; font-weight: bold;">$FromArgs</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Don't do anything here ... it just confuses things</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: #666666; font-style: italic;"># Normal &quot;run-once&quot; END processing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;End $Color&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 />
&nbsp;</div>

	<h4>A test case</h4>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Test Script:</span><br />
<span style="color: #666666; font-style: italic;">##</span><br />
<span style="color: #666666; font-style: italic;">## &quot;a&quot;,&quot;b&quot;,&quot;c&quot; | Test-PipelineV2 &quot;Cyan&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) | Test-PipelineV2 &quot;Cyan&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## Test-PipelineV2 &quot;Cyan&quot; @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) -verbose </span><br />
<span style="color: #666666; font-style: italic;">## </span><br />
<span style="color: #666666; font-style: italic;">## &quot;a&quot;,&quot;b&quot;,&quot;c&quot; | Test-PipelineV2 &quot;Cyan&quot; -verbose | Test-PipelineV2 &quot;Green&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) | Test-PipelineV2 &quot;Cyan&quot; -verbose | Test-PipelineV2 &quot;Green&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">## Test-PipelineV2 &quot;Cyan&quot; @(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) -verbose &nbsp;| Test-PipelineV2 &quot;Green&quot; -verbose</span><br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Expected Output (sorry, no color here...)</span><br />
<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Green<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Green<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Green<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Green<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Green<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">Begin</span> Cyan<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: a<br />
a<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: b<br />
b<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
<span style="color: #666699; font-weight: bold;">Process</span>: c<br />
c<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Cyan<br />
VERBOSE: <span style="color: #666699; font-weight: bold;">End</span> Green</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-20-ctp-script-cmdlets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

