Posts
47
Comments
55
Trackbacks
0
A strict shell is sometimes a good shell

Sorry for yet another hiatus, times have been busy. To make up for it, here is one of my favorite improvements in PowerShell (BTW: this change was driven partly by REAL IT administrators at Microsoft---thanks folks, you rock!). So here goes. Do you know that in a normal session of PowerShell, unassigned variables are really "null" by default? This is an advantage for folks who love PERL and other Unix-y tools as they can use this assumption to their advantage. But, it also has potential pitfalls---let's say you make one minor mistake in a script and type $myvariabel instead of $myvariable. Well you can imagine the longer the script, the longer the debugging session. So, there is a mode available in PowerShell, thankfully, which lets you be more strict in how the shell treats unassigned variables. Note the behavior change: Default behavior:

PS> $foo
PS>
PS> $foo -eq $null
True

See the problem? No? Well, I'll let you write some complex scripts and get back to me on that one. Here's how you turn off this loose behavior:

PS> set-psdebug -strict
PS> $foo
The variable $foo cannot be retrieved because it has not been set yet. At line:1 char:4 + $foo <<<< PS> $foo "is the best variable!" The variable $foo cannot be retrieved because it has not been set yet. At line:1 char:4 + $foo <<<<

Use it to your advantage, but since it does change the way the shell interprets variables, learn to use it before turning it on everywhere. I recommend turning it on during scrip execution at first.

posted on Saturday, October 14, 2006 8:10 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 4 and 7 and type the answer here:
News
A little slow these days as I'm busy working on exchangelabs.com. I will try and post tidbits when I get some time. Enjoy the older posts till then!