<?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; Debugging</title>
	<atom:link href="http://joelbennett.net/tag/debugging/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</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='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerShell: Determine your function&#8217;s position in the pipeline</title>
		<link>http://joelbennett.net/powershell-determine-your-functions-position-in-the-pipeline/</link>
		<comments>http://joelbennett.net/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://joelbennett.net/powershell-determine-your-functions-position-in-the-pipeline/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

