<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>PowerShell Station</title>
	
	<link>http://powershellstation.com</link>
	<description>Mike's PowerShell Musings</description>
	<lastBuildDate>Mon, 06 Sep 2010 18:20:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/PowershellStation" /><feedburner:info uri="powershellstation" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>My new favorite cmdlet: set-strictmode</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/NBuaPNAi9mQ/</link>
		<comments>http://powershellstation.com/2010/08/18/my-new-favorite-cmdlet-set-strictmode/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 19:42:50 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=304</guid>
		<description><![CDATA[If you’ve ever written Visual Basic or VB.Net code, you’re aware that it’s highly recommended that you use “Option Strict” in all of your code.&#160; Similarly, Perl scripters have a “use strict” that comes highly suggested. The idea of these options is that there’s quite a bit of flexibility built into these languages, and sometimes [...]]]></description>
			<content:encoded><![CDATA[<p>If you’ve ever written Visual Basic or VB.Net code, you’re aware that it’s highly recommended that you use “Option Strict” in all of your code.&#160; Similarly, Perl scripters have a “use strict” that comes highly suggested.</p>
<p>The idea of these options is that there’s quite a bit of flexibility built into these languages, and sometimes that flexibility backfires on you.&#160; Using these options actually limits the flexibility of the languages in question in ways that help coders to keep from making certain types of mistakes in their code.&#160; Writing code professionally in VB.Net (you may scoff, but it happens quite a lot) or Perl (it’s not my language of choice, but again, there’s a lot out there) without using these options is not a good idea at all.</p>
<p>I’ve been writing code in PowerShell for about 2 years, and have probably written about 10,000 lines, mostly code that’s used on a daily basis.&#160; I’ve read several PowerShell books, online tutorials, and watched several webcasts.&#160; I may have not been paying attention, but I’ve completely missed any emphasis on the analogous option in PowerShell, “set-strictmode”.&#160; One of my co-workers asked if there was something like this in PowerShell, and I found it almost instantly.&#160; Google, and the PowerShell help file both explain how the cmdlet works.&#160; He and I started using it, and to our dismay, found dozens of errors in code that we had been trusting, in some case, for over a year.</p>
<p>First of all, let me explain that I’m not complaining that there’s not any information about how set-strictmode works in the “ecology” of the PowerShell community.&#160; I’m confident that each book and tutorial explains how this cmdlet works.&#160; What I’m concerned about is how I could have read as much as I have from as many people are talking about PowerShell without hearing anyone (or everyone) shouting at the top of their collective voices, “Add this to your profile…it will save you countless hours and tears in the long run”.&#160; Hopefully, I’ve just missed it, and everyone has been saying this all along.&#160; In case this has not been the case, let me say:</p>
<p><span style="font-size: x-large">Add set-strictmode to your profile.</span></p>
<p><span style="font-size: x-large">It will save you countless hours and tears in the long run.</span></p>
<p>So….how do I use set-strictmode, and what does it help with?&#160; First, to turn on “strict mode” you need to decide which version of strictmode you want.&#160; The options are:</p>
<table border="1" cellspacing="0" cellpadding="2" width="565">
<tbody>
<tr>
<td valign="top" width="200">Version</td>
<td valign="top" width="363">Effect</td>
</tr>
<tr>
<td valign="top" width="200">1.0</td>
<td valign="top" width="363">References to uninitialized variables(except in a string) are errors</td>
</tr>
<tr>
<td valign="top" width="200">2.0</td>
<td valign="top" width="363">
<ul>
<li>References to uninitialized variables(including in a string) are errors </li>
<li>References to undefined properties are errors </li>
<li>Calling a function using method-call syntax is an error </li>
<li>${} (an un-named variable) is a syntax error </li>
</ul>
</td>
</tr>
<tr>
<td valign="top" width="200">Latest</td>
<td valign="top" width="363">Uses the most strict mode available for this version of PowerShell.&#160; (Currently the same as 2.0)</td>
</tr>
</tbody>
</table>
<p>I would advise that you use “set-strictmode –version Latest” in your profile.</p>
<p>Let’s look at the different restrictions.&#160; First, if you’ve written any PowerShell scripts, you’re aware that you don’t have to declare your variables.&#160; That’s a “good thing”, and strictmode 1.0 doesn’t change that in any way.&#160; What it does do is make sure that you’re not retrieving values from variables you haven’t assigned anything to.&#160; Here’s some sample code that strictmode will choke on.</p>
<pre class="brush: powershell">$servername=read-host -prompt &quot;Enter name of server&quot;
$wmi=get-wmiobject -class Win32_Service -computername $server_name</pre>
<p>This is a pretty simple example, but it’s almost certain that sooner or later you’ll typo a variable name in a script.&#160; Strictmode will help keep you from getting in this kind of trouble.&#160; With strictmode Version 1.0, you won’t be warned about using uninitialized variables in double-quoted string, e.g. “Server=$server_name”…(they’ll be replaced with $null).&#160; Version 2.0 will cause references to uninitialized variablesinside strings to be an error as well.</p>
<p>Here’s an example of code that illustrates property restrictions in StrictMode 2.0.</p>
<pre class="brush: powershell">$servername=&quot;Server123&quot;
$length=$servername.lenght</pre>
<p>I would write up an example of using the method-call syntax for a function, but Thomas Lee just <a href="http://tfl09.blogspot.com/2010/09/calling-functions-in-powershell.html">blogged about this</a> in his blog <a href="http://tfl09.blogspot.com/">Under the Stairs</a>.</p>
<p>The final “restriction” in strict mode 2.0 says that you can’t have a variable with no name.&#160; The syntax ${varname} is a way of expressing $varname, when varname contains characters that are not generally allowed in variable names.&#160; I haven’t ever had a really convincing use for this syntax (I’ve thought about using it to associate variables with filenames using the full path as the variable name, but it seems like a dictionary would be cleaner).</p>
<p>I should note that <a href="http://leeholmes.com/blog/">Lee Holmes</a> has done a great thing in his recently published <a href="http://www.leeholmes.com/blog/PowerShellCookbookV2NowAvailable.aspx">PowerShell Cookbook V2</a>.&#160; Each script in the cookbook starts with “set-strictmode –version Latest”.&#160;&#160; In my opinion, this is a best practice.</p>
<p>If you’re just now hearing about set-strictmode for the first time, and already have a body of powershell scripts in your toolbox (I’m in this category), I’d recommend the following steps:</p>
<ol>
<li>Add set-strictmode –version Latest to the profile on the machine you do your script development on</li>
<li>Add set-strictmode –version Latest&#160; to any scripts you create from this time forward</li>
<li>Add set-strictmode –version Latest to scripts that you have tested to make sure they comply with strictmode.</li>
</ol>
<p>I’m going to avoid turning on strictmode on servers that I’m running PowerShell on for now, because I’m sure stuff will break.&#160; I’m slowly refurbishing scripts as I run them on my development box and see the strictmode errors pop up, and since set-strictmode is a scoped change (if you set it in a script or function, it reverts to the old setting when you exit the script or function), I can take advantage of it in a script-by-script basis.</p>
<p>If you are just starting to build a PowerShell script library, I would recommend turning strictmode on in the profile on every machine that you run PowerShell scripts.&#160; You’ll be glad you did.</p>
<p>Hopefully this was not too much of a rant.&#160; Please let me know what experiences you’ve had with with set-strictmode.</p>
<p>Mike</p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F18%2Fmy-new-favorite-cmdlet-set-strictmode%2F&amp;linkname=My%20new%20favorite%20cmdlet%3A%20set-strictmode" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/NBuaPNAi9mQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/08/18/my-new-favorite-cmdlet-set-strictmode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/08/18/my-new-favorite-cmdlet-set-strictmode/</feedburner:origLink></item>
		<item>
		<title>PowerShell and MongoDB</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/wVbtztny3xo/</link>
		<comments>http://powershellstation.com/2010/08/10/powershell-and-mongodb/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 02:23:31 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=293</guid>
		<description><![CDATA[I recently saw this link on using NoSQL with Windows.  Now, I&#8217;m a SQL Server DBA, so I haven&#8217;t really had any reason to use NoSQL.  I was curious about how easy it was going to be to set up and if I could get it working with PowerShell. I selected MongoDB from the list [...]]]></description>
			<content:encoded><![CDATA[<p>I recently saw <a title="this link" href="http://www.dotnetconvo.com/post/view/2010/8/9/nosql-on-the-microsoft-platform">this link</a> on using NoSQL with Windows.  Now, I&#8217;m a SQL Server DBA, so I haven&#8217;t really had any reason to use NoSQL.  I was curious about how easy it was going to be to set up and if I could get it working with PowerShell.</p>
<p>I selected MongoDB from the list because it looked more like something that would be used on smaller-scale projects.</p>
<p>I then googled &#8220;MongoDB PowerShell&#8221; and found <a title="this link" href="http://www.dougfinke.com/blog/index.php/2009/10/25/how-to-use-mongodb-from-powershell-and-f/">this link</a> from Doug Finke about using MongoDB with PowerShell (and F#, which is another &#8220;cool thing&#8221; I haven&#8217;t managed to find a need for).  Doug links to <a title="another article" href="http://odetocode.com/blogs/scott/archive/2009/10/13/experimenting-with-mongodb-from-c.aspx">another article</a> which explains setting up MongoDB and an open-source .Net driver for MongoDB called <a title="mongo-csharp" href="http://github.com/samus/mongodb-csharp">mongo-csharp</a>.  He then follows up with a straight-forward script showing simple usage of MongoDB.  It looks like an almost literal translation of the C# code from the article he references.  With those in hand, I thought it was going to be a slam dunk.</p>
<p>It was, but I had a few hurdles to get over before I could get it working.  There weren&#8217;t any problems with the code;  it was written about a year ago, so it was using PowerShell 1.0 and an older version of mongo-csharp.  I had to update the script in a couple of places to make it work.  I probably wouldn&#8217;t even write it up, given how minor the changes are, but I was somewhat disappointed with the number of hits I got for &#8220;MongoDB PowerShell&#8221;.</p>
<p>Here&#8217;s the updated script:</p>
<pre class="brush: powershell">add-type -Path .\MongoDB.Driver.dll

$mongo=new-object mongodb.driver.mongo
$mongo.connect()
$db=$mongo.GetDataBase("movieReviews")
$movies=$db.GetCollection('Movies')

$movie=new-object Mongodb.driver.document
$movie['title']='Star Wars'
$movie['releaseDate']=get-date
$movies.Insert($movie)

$spec=new-object Mongodb.driver.document
$spec['title']='Star Wars'
$movies.FindOne($spec)
</pre>
<p>The two changes were</p>
<ol>
<li>added the parameter name (-path) to add-type, since the default param is looking for an assembly name, not a path to a DLL</li>
<li>changed $mongo.GetDB to $mongo.GetDatabase to reflect a change in the driver</li>
</ol>
<p>Worked fine.  Not sure how I&#8217;ll use this, but if I need to, I know I can from PowerShell.</p>
<p>-Mike</p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F10%2Fpowershell-and-mongodb%2F&amp;linkname=PowerShell%20and%20MongoDB" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/wVbtztny3xo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/08/10/powershell-and-mongodb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/08/10/powershell-and-mongodb/</feedburner:origLink></item>
		<item>
		<title>The Identity Function</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/6VpT8gZ_RqM/</link>
		<comments>http://powershellstation.com/2010/08/01/the-identity-function/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 02:44:20 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=284</guid>
		<description><![CDATA[In mathematics, an identity function is a function that returns the arguments that are passed to it unchanged.  While the concept of an identity function is quite often useful in formulating proofs, it is not something that I ever expected to use in a programming environment.   Here&#8217;s the identity function written in PowerShell: function identity{ [...]]]></description>
			<content:encoded><![CDATA[<p>In mathematics, an <a title="identity function" href="http://en.wikipedia.org/wiki/Identity_function">identity function</a> is a function that returns the arguments that are passed to it unchanged.  While the concept of an identity function is quite often useful in formulating proofs, it is not something that I ever expected to use in a programming environment.   Here&#8217;s the identity function written in PowerShell:</p>
<pre class="brush:powershell">
function identity{
    return $args
}
</pre>
<p>The surprising thing about this function is that it&#8217;s actually pretty useful.  PowerShell&#8217;s array literal syntax is considerably more involved than it&#8217;s argument syntax.  Using this function lets us do this:</p>
<pre class="brush:powershell">identity arg1 arg2 arg3</pre>
<p>instead of this:</p>
<pre class="brush:powershell">@('arg1','arg2','arg3')</pre>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F08%2F01%2Fthe-identity-function%2F&amp;linkname=The%20Identity%20Function" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/6VpT8gZ_RqM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/08/01/the-identity-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/08/01/the-identity-function/</feedburner:origLink></item>
		<item>
		<title>New Versions of PowerShell Community Extensions (PSCX) and SQL PowerShell Extensions (SQLPSX)</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/5lLm1qPpRB4/</link>
		<comments>http://powershellstation.com/2010/05/19/new-versions-of-pscx-and-sqlpsx/#comments</comments>
		<pubDate>Wed, 19 May 2010 13:00:37 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=279</guid>
		<description><![CDATA[In case you haven&#8217;t heard, the PowerShell Community Exetensions (PSCX) and SQL PowerShell Extensions (SQLPSX) projects have both recently released version 2.0 (and each followed shortly after with quick bug fixes). Both 2.0 releases are module-based and include advanced functions to solve lots of frequently encountered problems. If you haven&#8217;t ever used these toolsets, I [...]]]></description>
			<content:encoded><![CDATA[<p>In case you haven&#8217;t heard, the PowerShell Community Exetensions (PSCX) and SQL PowerShell Extensions (SQLPSX) projects have both recently released version 2.0 (and each followed shortly after with quick bug fixes).  Both 2.0 releases are module-based and include advanced functions to solve lots of frequently encountered problems.  If you haven&#8217;t ever used these toolsets, I would recommend giving them a try.</p>
<p><a href="http://pscx.codeplex.com">PowerShell Community Extensions</a></p>
<p><a href="http://sqlpsx.codeplex.com/">SQL PowerShell Extensions</a></p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F19%2Fnew-versions-of-pscx-and-sqlpsx%2F&amp;linkname=New%20Versions%20of%20PowerShell%20Community%20Extensions%20%28PSCX%29%20and%20SQL%20PowerShell%20Extensions%20%28SQLPSX%29" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/5lLm1qPpRB4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/05/19/new-versions-of-pscx-and-sqlpsx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/05/19/new-versions-of-pscx-and-sqlpsx/</feedburner:origLink></item>
		<item>
		<title>Passing Predicates as Parameters in PowerShell</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/kAnYODPYEsE/</link>
		<comments>http://powershellstation.com/2010/05/18/passing-predicates-as-parameters-in-powershell/#comments</comments>
		<pubDate>Wed, 19 May 2010 02:30:33 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=275</guid>
		<description><![CDATA[This is just a quick trick that I figured out today. I had a process that manipulated a dataset, and I needed to be able to change the process to allow me to filter the data that was processed. Also, it wasn&#8217;t clear exactly what kind of filter would specifically be needed in any given [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick trick that I figured out today.  I had a process that manipulated a dataset, and I needed to be able to change the process to allow me to filter the data that was processed.  Also, it wasn&#8217;t clear exactly what kind of filter would specifically be needed in any given scenario.</p>
<p>Normally, I would just filter the data using <strong>where-object</strong> and pass it in to the function in question.  The problem here was that the data retrieval was somewhat cumbersome, and I didn&#8217;t want to push that complexity outside of the function.  And since the filtering criteria wasn&#8217;t clear-cut, I couldn&#8217;t (and didn&#8217;t want to) use a bunch of switches and parameters along with a nest of if/else conditions.</p>
<p>What I wanted, was to pass a predicate (an expression that would evaluate to true or false depending on whether I want a row in the dataset) in to the function.  Essentially, I wanted to insert a <strong>where-object</strong> into the middle of the function.</p>
<p>Amazingly, PowerShell allows me to do that.  The code looks a bit strange to me at first, but it works very well and isn&#8217;t complicated at all.</p>
<p>Here&#8217;s an example:</p>
<pre class="brush: powershell">
function process-data{
Param( [scriptblock]$filter = {$true})

	#retrieve the data

	#filter the data
	$data = $data | where-object $filter

	#process the data
}

process-data -filter {$_.UpdatedDateTime -gt (get-date '1/1/2010')}
</pre>
<p>It&#8217;s not earth-shattering, but I think this will come in handy in several places for me.</p>
<p>Let me know what you think.</p>
<p>-Mike</p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F05%2F18%2Fpassing-predicates-as-parameters-in-powershell%2F&amp;linkname=Passing%20Predicates%20as%20Parameters%20in%20PowerShell" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/kAnYODPYEsE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/05/18/passing-predicates-as-parameters-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/05/18/passing-predicates-as-parameters-in-powershell/</feedburner:origLink></item>
		<item>
		<title>Checking a Field for NULL in PowerShell</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/2SHQ7zPdfFY/</link>
		<comments>http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 03:45:30 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[NULL]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/</guid>
		<description><![CDATA[It’s been a long time (over 2 months) since I last posted.  I’ll try to get back into a rhythm of posting at least weekly.  Anyway, this is something that occurred to me at work when writing a script. I usually avoid nullable columns, but sometimes date fields make sense to be null (rather than [...]]]></description>
			<content:encoded><![CDATA[<p>It’s been a long time (over 2 months) since I last posted.  I’ll try to get back into a rhythm of posting at least weekly.  Anyway, this is something that occurred to me at work when writing a script.</p>
<p>I usually avoid nullable columns, but sometimes date fields make sense to be null (rather than use sentinel values like 1/1/1900).  In this case, I had a nullable date column and I needed to check in PowerShell whether the field was in fact null or not.  In SQL, I would have just used an IS NULL, or used the IsNull() function to replace the null value with something a little easier to deal with.  My first (feeble) attempt was to do this:</p>
<pre class="brush:powershell">if (!$_.completedDate){
# it’s null
}</pre>
<p>Unfortunately for me, that doesn’t work.  Next, I used this (which worked, but wasn’t very satisfactory either):</p>
<pre class="brush:powershell">if ($_.completedDate.ToString() -eq ''){
# it’s null
}</pre>
<p>Realizing that I was being stupid, I googled “PowerShell SQL NULL and after looking at several pages which didn’t really address the issue, I found <a href="http://blogs.technet.com/industry_insiders/pages/testing-for-database-null-values-from-powershell.aspx">this.</a> A little work to change it into a function, and voilà.</p>
<pre class="brush:powershell">function is-null($value){
  return  [System.DBNull]::Value.Equals($value)
}</pre>
<p>A few quick tests and this is what I wanted. Now, my code looks like this:</p>
<pre class="brush:powershell">if (is-null $_.completedDate){
# it’s null
}</pre>
<p>I find it hard to believe I haven’t written this function before (or seen it).</p>
<p>By the way…be watching the <a href="http://sqlpsx.codeplex.com">SQL PowerShell Extensions</a> project.  Chad released version 2.1, which includes SQL mode for the ISE (really nice).  I know he and several others are collaborating on an update which should be out sometime soon.</p>
<p>-Mike</p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F04%2F09%2Fchecking-a-field-for-null-in-powershell%2F&amp;linkname=Checking%20a%20Field%20for%20NULL%20in%20PowerShell" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/2SHQ7zPdfFY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/04/09/checking-a-field-for-null-in-powershell/</feedburner:origLink></item>
		<item>
		<title>The PowerShell Bug That Wasn’t, and More Package Management</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/64vPDuq3K4k/</link>
		<comments>http://powershellstation.com/2010/01/20/the-powershell-bug-that-wasnt-and-more-package-management/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 05:04:26 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Discussion]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[V2.0 Features]]></category>
		<category><![CDATA[Package]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Require]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=255</guid>
		<description><![CDATA[Have you ever tracked down a bug, been confident that you had found the root of your problems, only to realize shortly afterwords that you missed it completely? What I posted yesterday as a bug in PowerShell (having to do with recursive functions, dot-sourcing, and parameters) seemed during my debugging session to clearly be a [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever tracked down a bug, been confident that you had found the root of your problems, only to realize shortly afterwords that you missed it completely?</p>
<p>What I posted yesterday as a bug in PowerShell (having to do with recursive functions, dot-sourcing, and parameters) seemed during my debugging session to clearly be a bug.  After all, I watched the parameter value change from b to a, didn&#8217;t I?  Sure did.  And in almost every language I&#8217;ve ever used, that would be a bug.  On the other hand, PowerShell is the only language that I know of that has dot-sourcing.  Here&#8217;s a much simpler code example which shows my faulty thinking:</p>
<pre class="brush: powershell">function f($x){
   if ($x -eq 1){
      write-host $x
      . f ($x+1)
      write-host $x
   }
}

f 1</pre>
<p>Here, we have a simple &#8220;recursive function&#8221; which uses dot-sourcing to call itself.  In my mind, how this would have worked is as follows:</p>
<ul>
<li>We call the function, passing 1 for $x</li>
<li>The if condition is true, so it prints 1 and calls the function, passing 2 for $x</li>
<li>In the inner call, the if condition is false, so nothing happens</li>
<li>We pop back to the calling frame, where $x is 1 and print it</li>
</ul>
<p>If it weren&#8217;t for that pesky dot operator, that would have been accurate.</p>
<p>The problem is, the dot operator changes the scoping of the inner call.  Here&#8217;s what the about_operators help topic, has to say about the dot sourcing operator:</p>
<pre>        Description: Runs a script so that the items in the script are part of the calling scope.</pre>
<p>Which is not a surprise&#8230;really.  The reason I was using the dot operator in my package management code was to make sure that functions defined in the scripts it was calling would be included in the existing scope, rather than their script scope.  The problem was one of nearsightedness.  I was so focused on the fact that the dot sourcing was making the functions part of the caller&#8217;s scope that I didn&#8217;t consider that variable declarations (including parameters) would also be in the caller&#8217;s scope.</p>
<p>So, the correct interpretation of the above script is:</p>
<ul>
<li>We call the function, passing 1 for $x</li>
<li>The if condition is true, so it prints 1 and calls the function, passing 2 for $x</li>
<li>The parameter is named $x, so $x in is set to 2 (overwriting the $x that was set to 1)</li>
<li>In the inner call, the if condition is false, so nothing happens</li>
<li>We pop back to the calling frame, where $x is 2 and print 2.</li>
</ul>
<p>The trick here is that the function f dot-sourced something that set $x to 2.  The fact that it was f is incidental.  It didn&#8217;t have to be.</p>
<p>Maybe this example will make it more clear:</p>
<pre class="brush:powershell">function f($x){
    write-host $x
    . g
    write-host $x
 }

function g{
   $x = "Hello, World!"
}
f 1</pre>
<p>If we were doing this without dot-sourcing, we would expect to see the number 1 printed out twice.  However, since we dot-sourced g, the assignment in the function body of g happens in the scope of f.  In other words, it&#8217;s as if the $x=&#8221;Hello, World!&#8221; were executed inside f.  Thus, the output of this script is 1, followed by &#8220;Hello, World!&#8221;.</p>
<p>So, it wasn&#8217;t a bug, it was just me not being thorough in applying my understanding of dot-sourcing.</p>
<p>Now, on with Package Management.<br />
First, to fix the problem caused by the parameter being overwritten (which it is, it&#8217;s just that it&#8217;s expected to be).  I hadn&#8217;t worked out a way to fix the problem before I went to bed last night, but as I was rolling this stuff around in my head (which is when I figured out that it wasn&#8217;t really a <em>bug</em>), I thought of a simple solution.  Since we can expect that sometimes the $filename parameter in the require (and reload) function will be overwritten by the a value in the dot-sourced script, we just need to make sure we&#8217;re done using it at that point.  So, I simply made the assignment to the dictionary before dot-sourcing.   Here&#8217;s the updated code:</p>
<pre class="brush: powershell">$global:loaded_scripts=@{pkg_utils='INITIAL'}

function require($filename){
	if (!$global:loaded_scripts[$filename]){
	   $global:loaded_scripts[$filename]=get-date
	   . scripts:\$filename.ps1
	}
}
function reload($filename){
	$global:loaded_scripts[$filename]=get-date
	. scripts:\$filename.ps1
}</pre>
<p>To add modules, we need to do a few extra things:</p>
<ul>
<li>We need to detect if we&#8217;re running in 2.0 or not</li>
<li>We need to see if there is a module with the given name</li>
<li>We need to see if the module is already loaded or not (in the case of require&#8230;it won&#8217;t matter for reload</li>
</ul>
<p>Fortunately, none of those are very difficult.  Here&#8217;s the updated code (including modules).  I even added some comments to make the flow more clear:</p>
<pre class="brush:powershell">
$global:loaded_scripts=@{pkg_utils='INITIAL'}

function require($filename){
    if ($global:loaded_scripts[$filename]){
          # this function has already loaded this (script or module)
          return
    }
    if ($psversiontable){
       # we're in 2.0
       if (get-module $filename -listavailable){
               #the module exists in the module path
         	   $global:loaded_scripts[$filename]=get-date
               import-module $filename
               return
       }
    }
    #it wasn't a module...so dot-source the script
    $global:loaded_scripts[$filename]=get-date
    . scripts:\$filename.ps1

}
function reload($filename){
    if ($psversiontable){
        # we're in 2.0
        if (get-module $filename -listavailable){
           #the module exists in the module path
           $global:loaded_scripts[$filename]=get-date
           import-module $filename
           return
        }
    }
    # it wasn't a module...so dot-source the script.
  	$global:loaded_scripts[$filename]=get-date
	. scripts:\$filename.ps1
}
</pre>
<p>That&#8217;s it for today.  Let me know what you think.</p>
<p>-Mike</p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F20%2Fthe-powershell-bug-that-wasnt-and-more-package-management%2F&amp;linkname=The%20PowerShell%20Bug%20That%20Wasn%26%238217%3Bt%2C%20and%20More%20Package%20Management" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/64vPDuq3K4k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/01/20/the-powershell-bug-that-wasnt-and-more-package-management/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/01/20/the-powershell-bug-that-wasnt-and-more-package-management/</feedburner:origLink></item>
		<item>
		<title>Package Management and a PowerShell Bug</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/idhphB5I92g/</link>
		<comments>http://powershellstation.com/2010/01/19/package-management-and-a-powershell-bug/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 05:41:58 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Discussion]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Package]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Require]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=237</guid>
		<description><![CDATA[UPDATE: I have worked out how the behavior described at the end of this post is not a bug, but in fact just PowerShell doing what it&#8217;s told. Don&#8217;t have time to explain right now, but I&#8217;ll write something up later today. I also worked out how to &#8220;fix&#8221; the behavior. For a long time [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:  I have worked out how the behavior described at the end of this post is not a bug, but in fact just PowerShell doing what it&#8217;s told.  Don&#8217;t have time to explain right now, but I&#8217;ll write something up later today.  I also worked out how to &#8220;fix&#8221; the behavior.</strong></p>
<p>For a long time now, I&#8217;ve been dissatisfied with what I call &#8220;package management&#8221; in PowerShell.  Those of you who know me will be shocked that anything in PowerShell is less than perfect in my eyes, but this is one place that I feel let down.  Modules in 2.0 remedy the situation somewhat, but it still isn&#8217;t quite what I want or am used to in other languages.</p>
<p>Let me give an example.  In VB.NET, if you need to use the functions in an assembly, you put &#8220;Imports AssemblyName&#8221; at the top of your script.  In C#, you would have &#8220;Using AssemblyName&#8221;.  In Python, there would be &#8220;Import Something&#8221;.</p>
<p>In PowerShell 1.0, you had nothing.  In 2.0, you could create a module manifest which would specify either RequiredModules or ScriptsToProcess (or several other things to do upon loading the module).  The problems I see  with using the module manifest are:</p>
<ul>
<li>What if I&#8217;m not writing a module?  There&#8217;s no such thing as a &#8220;script manifest&#8221;</li>
<li>What if the script or module that is required performs some initialization that should only be done once per session?</li>
<li>What if the script or module that is required performs <em>expensive</em> initialization?</li>
</ul>
<p>Because of these reasons (and because I only started using 2.0 when it went RTM) I wrote a couple of quick functions to do what I thought made sense.</p>
<pre class="brush: powershell">
$global:loaded_scripts=@{pkg_utils='INITIAL'}

function require($filename){
	if (!$global:loaded_scripts[$filename]){
	   . scripts:\$filename.ps1
	   $global:loaded_scripts[$filename]=get-date
	}
}
function reload($filename){
	. scripts:\$filename.ps1
	$global:loaded_scripts[$filename]=get-date
}
</pre>
<p>To use these you need to create a psdrive called scripts: with code like this (probably in your profile):</p>
<pre class="brush:powershell">New-PSdrive -name scripts -PSprovider filesystem -root \\PathToYourLibraries | Out-Null</pre>
<p>Then, also in your profile, you&#8217;ll want to dot-source the file you put these functions in (for example, package_tools.ps1):</p>
<pre class="brush: powershell">. scripts:\package_tools.ps1</pre>
<p>Once you have those set up, you can dot-source the <strong>require</strong> function to make sure that a script has been loaded as such:</p>
<pre class="brush:powershell">. require somelibrary</pre>
<p>I have the functions I use divided by &#8220;subject&#8221; into several library scripts, and make sure that at the top of each script, I use &#8220;. require&#8221; to ensure that any prerequisites are already loaded.</p>
<p>Now for the PowerShell bug (which took me a long time to track down).<br />
Create 2 files, a.ps1 and b.ps1 in your scripts: directory. </p>
<pre class="brush:powershell">
# a.ps1
write-host "this is script a"
</pre>
<pre class="brush: powershell">
#b.ps1
write-host "this is script b"
write-host "this script loads a"
. require a
</pre>
<p>After dot-sourcing package_tools, run the following commands:</p>
<pre class="brush: powershell">
. require b
</pre>
<p>You should get output that looks something like this:</p>
<pre>
this is script b
this script loads a
this is script a
</pre>
<p>Everything looks good until you inspect the $global:loaded_scripts variable:</p>
<pre>
ps> $loaded_scripts

Name                           Value
----                           -----
a                              1/19/2010 11:23:09 PM
package_tools                  INITIAL
</pre>
<p>Although b.ps1 was indeed dot-sourced (you can see the output), and the only code-path through the  require function that would dot-source it would also add an entry to $loaded_scripts, there is no such entry.  The problem is that when b.ps1 called the require function (to load a.ps1), the $filename variable in the calling context (where it should have been &#8220;b&#8221;) was overwritten by the call with &#8220;a&#8221; as a parameter.  Walking through the code in a debugger confirms the problem.  </p>
<p>Have you ever seen problems with recursion and dot-sourcing in PowerShell?  Can you see any way around the problem I&#8217;ve described?  For instance, saving the $filename in a variable and restoring it after the dot-source call (line 5 above) doesn&#8217;t help, because the same code-path is followed in the recursive call, and that variable is overwritten as well.</p>
<p>Even with this bug, I find the require function (and reload, which I didn&#8217;t discuss, but always loads the script in question) to be very helpful.  I also have extended them to include importing modules, if they exist.  I&#8217;ll discuss them in my next post, coming soon.</p>
<p>-Mike</p>
<p>P.S.  <a href="http://stackoverflow.com/questions/279974/importing-libraries-in-powershell">Here</a>&#8216;s a question I posted to StackOverflow.com about these functions back in November of 2008.</p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F19%2Fpackage-management-and-a-powershell-bug%2F&amp;linkname=Package%20Management%20and%20a%20PowerShell%20Bug" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/idhphB5I92g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/01/19/package-management-and-a-powershell-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/01/19/package-management-and-a-powershell-bug/</feedburner:origLink></item>
		<item>
		<title>SQL PowerShell Extensions (SQLPSX) 2.0 Released</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/U3D7tkge_5g/</link>
		<comments>http://powershellstation.com/2010/01/05/sql-powershell-extensions-sqlpsx-2-0-released/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 00:39:17 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Script]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=233</guid>
		<description><![CDATA[The first module-based release of the SQL PowerShell Extensions (SQLPSX) was released recently on CodePlex.  It features very handy wrappers for most of the SMO objects used to manipulate SQL Server metadata, SSIS packages, Replication, and (new in the 2.0 release) an ADO.NET module which I wrote based on the code in this post.  There&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>The first module-based release of the SQL PowerShell Extensions (SQLPSX) was released recently on CodePlex.  It features very handy wrappers for most of the SMO objects used to manipulate SQL Server metadata, SSIS packages, Replication, and (new in the 2.0 release) an ADO.NET module which I wrote based on the code in this <a href="http://powershellstation.com/2009/09/15/executing-sql-the-right-way-in-powershell/">post</a>.  There&#8217;s also a data-collection process and Reporting Services reports to help you get your SQL Server installations under control.</p>
<p>Chad Miller, the driving force behind SQLPSX, has put a lot of effort into this release, and you&#8217;ll find really good examples of advanced functions (with comment-based help, even).</p>
<p>If you deal with SQL Server in any way, you&#8217;ll almost certainly be able to use this set of modules to streamline your scripting experience (and probably learn something about SMO in the process).</p>
<p>You can find the release <a href="http://sqlpsx.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=38047">here</a>.</p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2010%2F01%2F05%2Fsql-powershell-extensions-sqlpsx-2-0-released%2F&amp;linkname=SQL%20PowerShell%20Extensions%20%28SQLPSX%29%202.0%20Released" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/U3D7tkge_5g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2010/01/05/sql-powershell-extensions-sqlpsx-2-0-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2010/01/05/sql-powershell-extensions-sqlpsx-2-0-released/</feedburner:origLink></item>
		<item>
		<title>Get-EventLog and Get-WMIObject</title>
		<link>http://feedproxy.google.com/~r/PowershellStation/~3/b3P2GAxvWcQ/</link>
		<comments>http://powershellstation.com/2009/12/16/get-eventlog-and-get-wmiobject/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 05:51:39 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://powershellstation.com/?p=209</guid>
		<description><![CDATA[Recently, we had an occasion to write a process to read event logs on several sql servers to try to determine login times for different sql and Windows logins.  Since we have begun using PowerShell v2.0, and since get-eventlog now has a -computername parameter, it seemed like an obvious solution. The event message we were [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, we had an occasion to write a process to read event logs on several sql servers to try to determine login times for different sql and Windows logins.  Since we have begun using PowerShell v2.0, and since get-eventlog now has a -computername parameter, it seemed like an obvious solution.</p>
<p>The event message we were interested in looked something like &#8220;Login succeeeded for uesr &#8216;UserName&#8217; &#8230;.&#8221;.  The code we were trying to use was:</p>
<pre class="brush: powershell">
get-eventlog -computername $servername -logname Application -message "Login succeeded for user*" -after ((get-date).AddDays(-1))
</pre>
<p>I expected that, given a date parameter and a leading string to match wouldn&#8217;t be too bad, but this ended up taking several minutes per server.  As there are over a hundred servers to scan, that didn&#8217;t work well for us.</p>
<p>We ended up falling back to get-wmiobject.</p>
<pre class="brush: powershell">
$BeginDate=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((get-date).AddDays(-1))
get-wmiobject -class win32_ntlogevent -computerName $servername -filter "(EventCode=18453)  and (LogFile='Application') and (TimeGenerated &gt;'$BeginDate')"
</pre>
<p>Cons:</p>
<ul>
<li><span style="color: #000000;">We have to encode the date parameter (instead of using a nice datetime parameter like get-eventlog has)</span></li>
<li><span style="color: #000000;">We have to write a WQL where-clause to match the parameters<br />
</span></li>
</ul>
<p><span style="color: #000000;">Pros:</span></p>
<ul>
<li><span style="color: #000000;">We get to use the event code (rather than a string match)</span></li>
<li><span style="color: #000000;">The code is orders of magnitude faster (39 servers in 13 minutes as a test case)<br />
</span></li>
</ul>
<p><span style="color: #000000;"> I think that you might have a positive experience using get-eventlog if you need to scan a range of time (for instance if you&#8217;re reporting on what happened on the server), but if you need to look for a specific event (or set of events) you&#8217;re probably going to want to use get-wmiobject.</span></p>
<p><span style="color: #000000;">-Mike<br />
</span></p>
<p><a href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Digg" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a> <a href="http://www.addtoany.com/add_to/slashdot?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Slashdot" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/slashdot.png" width="16" height="16" alt="Slashdot"/></a> <a href="http://www.addtoany.com/add_to/reddit?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Reddit" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reddit.png" width="16" height="16" alt="Reddit"/></a> <a href="http://www.addtoany.com/add_to/tumblr?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Tumblr" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/tumblr.png" width="16" height="16" alt="Tumblr"/></a> <a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Delicious" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Twitter" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a> <a href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fpowershellstation.com%2F2009%2F12%2F16%2Fget-eventlog-and-get-wmiobject%2F&amp;linkname=Get-EventLog%20and%20Get-WMIObject" title="Google Reader" rel="nofollow" target="_blank"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://powershellstation.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p><img src="http://feeds.feedburner.com/~r/PowershellStation/~4/b3P2GAxvWcQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://powershellstation.com/2009/12/16/get-eventlog-and-get-wmiobject/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://powershellstation.com/2009/12/16/get-eventlog-and-get-wmiobject/</feedburner:origLink></item>
	</channel>
</rss>
