::::::::: PowerShell :::::::::
Thursday, April 27, 2006
  Option Explicit, I mean "Strict" switch...
I have been waiting for this feature for quite a while.
Setting Visual Basic's "Option Explict" statement like switch in PowerShell.

The Magic Word is..
Set-PSDebug -Strict
Upon executing the command, according to help page(Get-Help Set-PSDebug),
the interpreter will throw an exception if the referenced variable hasn’t been set yet.
Let's see when that feature might come in handy.
PS> Set-PSDebug -Off
PS> function add($x, $y) { $x + $z }
PS> add 1 10
1
PS> $z = 100
PS> add 1 10
101
As you can see, with "Strict" mode off(well "Strict" flag is of type Switch, and all the Boolean flags were replaced with Switches), the PowerShell interpreter simple accepts the fact that function "add" just adds value of $x and $z without caring about what the value of $z is.

Now, let's set the "Strict" option on
PS> Set-PSDebug -Strict
PS> ri function:add; rv z # remove "add" function and the variable $z
PS> function add($x, $y) { $x + $z }
PS> add 1 10
The variable $z cannot be retrieved because it has not been set yet.
At line:1 char:31
+ function add($x, $y) { $x + $z <<<< }
Although the demo is really too simple of a case, in your scripts, you might have variables that you are not keeping track of but being still used somewhere in your scripts.

Well, if you are using PowerShell just for interactive uses for a quick and dirty operations, forget about this feature but then if you are writing cmdlets, setting this option always would be a good idea.


Tags :
 
Comments: Post a Comment

Links to this post:

Create a Link



<< Home
Let's get lazy with PowerShell!

Name:
Location: Flushing, NY, United States

Experimenting with a different format of blogs...

Links
ARCHIVES
10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /


Powered by Blogger