<?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>Live PowerShell With Karl Prosser &#187; PS extended</title>
	<atom:link href="http://karlprosser.com/coder/category/ps-extended/feed/" rel="self" type="application/rss+xml" />
	<link>http://karlprosser.com/coder</link>
	<description>invoke-intelligence &#124; where { $_.necessary }</description>
	<lastBuildDate>Fri, 09 Jul 2010 03:39:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sick of an exception happening to just one item in the pipeline when you don&#8217;t care.</title>
		<link>http://karlprosser.com/coder/2009/11/13/sick-of-an-exception-happening-to-just-one-item-in-the-pipeline-when-you-dont-care/</link>
		<comments>http://karlprosser.com/coder/2009/11/13/sick-of-an-exception-happening-to-just-one-item-in-the-pipeline-when-you-dont-care/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 17:59:00 +0000</pubDate>
		<dc:creator>Karl</dc:creator>
				<category><![CDATA[PS extended]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">http://karlprosser.com/coder/2009/02/13/sick-of-an-exception-happening-to-just-one-item-in-the-pipeline-when-you-dont-care/</guid>
		<description><![CDATA[How many times have you had something like: get-thousandsofitems &#124; % { do-something $_ } &#124; whatever Only to have some exception happen in do-something and it throws the whole thing. When really you don’t care, you’d rather just be able to know what failed and have it move on. This happens a fair bit [...]]]></description>
			<content:encoded><![CDATA[<p>How many times have you had something like:    </p>
<p><strong>get-thousandsofitems | % { do-something $_ } | whatever</strong></p>
<p>Only to have some exception happen in do-something and it throws the whole thing. When really you don’t care, you’d rather just be able to know what failed and have it move on. This happens a fair bit with Get-WMIobject etc, because one of the machines may be off, firewall blocked, some sort of permissions issues – things that bubble up even when you have the preference set to SilentyContinue. </p>
<p>So what do we normally do in this scenario. We either pipeline it and make it look like a VBscript, or ruin our beautiful single liner by making that script complicated with V1 traps and the likes, and we usually get the error handling wrong wrong a number of times along the way.</p>
<p>Well with PowerShell you can always adjust and expand the language. There is nothing particular special about the foreach-object Cmdlet. In its common use you can replicate it with a function as simple as the one below.</p>
<p><strong>function myforeach-object([scriptblock]$process) { process { &amp;$process } }</strong></p>
<p>So how about making a looping function that deals with the exceptions for you, you say? Great Idea!, Now you are cooking. The following function is pretty much a simple V1 function, though it should really be completed and production readied into a V2 Advanced function with all the modern trimmings and trappings, however I DO use V2 TRY/CATCH syntax instead of V1 Trapping, because its more sane to us who don’t have a secret VBscript fetish.</p>
<pre><span style="color: #0000ff">function</span><span style="color: #000000"> </span><span style="color: #008080">foreach-withexception</span><span style="color: #000000"> ([scriptblock]</span><span style="color: #0000ff">$process</span><span style="color: #000000">,</span><span style="color: #0000ff">$outputexception</span><span style="color: #000000">)
{
  begin { </span><span style="color: #0000ff">$global:foreachex</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> @() }
  process {
            try
            {
            </span><span style="color: #0000ff">$local:inputitem</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">$_</span><span style="color: #000000">
            </span><span style="color: #000000">&amp;</span><span style="color: #0000ff">$process</span><span style="color: #000000">
            }
            catch
            {
            </span><span style="color: #0000ff">$local:exceptionitem</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #ff0000">1</span><span style="color: #000000"> </span><span style="color: #000000">|</span><span style="color: #000000"> </span><span style="color: #008080">select-object</span><span style="color: #000000"> </span><span style="color: #0000ff">object</span><span style="color: #000000">,exception
            </span><span style="color: #0000ff">$local:exceptionitem</span><span style="color: #000000">.</span><span style="color: #0000ff">object</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">$local:inputitem</span><span style="color: #000000">
            </span><span style="color: #0000ff">$local:exceptionitem</span><span style="color: #000000">.exception </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">$_</span><span style="color: #000000">
            </span><span style="color: #0000ff">$global:foreachex</span><span style="color: #000000"> </span><span style="color: #000000">+=</span><span style="color: #000000"> </span><span style="color: #0000ff">$local:exceptionitem</span><span style="color: #000000">
            }
          }
  end {}
}
</span><span style="color: #008080">set-alias</span><span style="color: #000000"> </span><span style="color: #000000">%</span><span style="color: #000000">ex </span><span style="color: #008080">foreach-withexception</span><span style="color: #000000">
</span></pre>
<p>So i love my aliases , %ex for foreach with exceptions. Really I wish i could come up with a better name for the long function. I also keep a %i for my foreach-index function as well. Basically you use this just like foreach-object (though i haven’t implemented a begin and end area in this), and exceptions get eaten, but after the fact you can access the object that caused the error, and the error in the global $foreachex variable. At a later date i’m going to add an option paramter variable that if can put the exception in instead of a naughty global variable. But as Jeffrey Snover says “To ship is to choose” though for me it really means “this function does what i need it to now, i need to go back and solve the Real problem i’m solving that this function really helps with”</p>
<p>here is an example</p>
<p><strong>100..-5 | %ex {&#160; &quot;yo $_&quot; ;&#160; 1 / $_ }<br />
    <br />$foreachex</strong></p>
<p><a href="http://karlprosser.com/coder/wp-content/uploads/2009/02/capture1.jpg"><img title="Capture1" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="293" alt="Capture1" src="http://karlprosser.com/coder/wp-content/uploads/2009/02/capture1-thumb.jpg" width="518" border="0" /></a> </p>
<p>normally 0/0 would throw a fuss , and kill the whole thing, but it just works on , and you can check to see if there are any errors in $foreachex Normally the things that i need this for though are “loose web services”, WMI, or networking sort of things. but really it simplifies my one liners, and gets things done quicker.</p>
<p><a href="http://www.poshcode.org/864">@poshcode</a> | <a href="http://www.poshcode.org/get/864">Download</a></p>
<p>&#160;</p>
<blockquote dir="ltr" style="margin-right: 0px"></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://karlprosser.com/coder/2009/11/13/sick-of-an-exception-happening-to-just-one-item-in-the-pipeline-when-you-dont-care/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
