This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
On my blog as I provide samples, I most of the time keep them as simple as possible,
without errorhandling or parameterchecking etc.I just focus on the target
as in the temperature converter function examples in last post,
http://mow001.blogspot.com/2006/05/powershell-celsius-fahrenheit.htmlI did them as one-liners without any checking to keep it simple.
Jeffrey Snover, was so kind to leave a variation in the Comments, that does give usage info if you do not give a parameter
(see example 2 below), mine would just convert 0
(see example 1 below)this makes the script a bit more userfriendly, but what if a string is given ?
FooFoo32 ?
hmm, we should better catch that also then
...(that is why I let them out most of the time, to keep focussed at the "real" example ;-), but ok lets provide 2 other variations now, to make it up) one way is to give typeinfo data to the parameter, so that this is checked by the function,
(see example 3 below)so we also did catch wrong parameters now.
but this does not give the usage information,
so I did another veriation, in the last example, I removed the typeinfo to the parameter and used a Trap Statement, to provide the UsageInformation
.(see example 4 below)in the samples below I will show the different version and what does happen with different kinds of (wrong) input :
Examples :
# Example 1
# My old Function :
function ConvertTo-Celsius ($Fahrenheit) {($Fahrenheit - 32) / 1.8}
# example
MowPS>ConvertTo-Celsius 77
25
# if you not specify a parameter $null -> 0 is calculated :
MowPS>ConvertTo-Celsius
-17.7777777777778
# example 2 :
# Jeffrey Snover 's variation with some errorhandling
function ConvertTo-Fahrenheit ($Celsius=$(Throw "USAGE: ConvertTo-Fahrenheit -Celsius degrees")) {
"{0:N1}" -f ($Celsius * 1.8 + 32)
}
function ConvertTo-Celsius ($Fahrenheit=$(Throw "USAGE: ConvertTo-Celsius -Fahrenheit degrees")) {
"{0:N1}" -f (($Fahrenheit - 32) / 1.8)
}
# output
# Usage information as no parameter is given,
MowPS>ConvertTo-Fahrenheit
USAGE: ConvertTo-Fahrenheit -Celsius degrees
At line:1 char:48
+ function ConvertTo-Fahrenheit ($Celsius=$(Throw <<<< "USAGE: ConvertTo-Fahrenheit -Celsius degrees")) {
# but if a string is given :
MowPS>ConvertTo-Fahrenheit foo
foofoo32
# Example 3
# we can catch this by adding typeinfo to the parameter :
function ConvertTo-Celsius ([decimal]$Fahrenheit=$(Throw "USAGE: ConvertTo-Celsius -Fahrenheit degrees")) {
"{0:N1}" -f (($Fahrenheit - 32) / 1.8)
}
# output
MowPS>ConvertTo-Celsius
USAGE: ConvertTo-Celsius -Fahrenheit degrees
At line:1 char:57
+ function ConvertTo-Celsius ([decimal]$Fahrenheit=$(Throw <<<< "USAGE: ConvertTo-Celsius -Fahrenheit degrees")) {
MowPS>ConvertTo-Celsius foo
ConvertTo-Celsius : Cannot convert value "foo" to type "System.Decimal". Error: "Input string was not in a correct format."
At line:1 char:18
+ ConvertTo-Celsius <<<< foo
MowPS>ConvertTo-Celsius 77
25.0
# Example 4 :
# an other variation giving a custom message using TRAP :
function ConvertTo-Fahrenheit ($Celsius=$(Throw "USAGE: ConvertTo-Fahrenheit -Celsius degrees")) {
trap{Throw "USAGE: ConvertTo-Fahrenheit -Celsius degrees"}
$Celsius=[decimal]$Celsius
"{0:N1}" -f ($Celsius * 1.8 + 32)
}
# output :
MowPS>ConvertTo-Fahrenheit foo
USAGE: ConvertTo-Fahrenheit -Celsius degrees
At line:2 char:13
+ trap{Throw <<<< "USAGE: ConvertTo-Fahrenheit -Celsius degrees"}
hope this examples help in adapting other examples to scripts with some errorchecking,
(so lazy me can leave them out in my samples and say it is for readability ;-))For more information about errorhandling and debugging in powershell see also this 7 part series on the PowerShell team blog about it :
Debugging Monad Scripts, Part 7 (Final): How Traps Work (links to all former parts are in this post)Enjoy,
Greetings /\/\o\/\/
Tags : Monad msh PowerShell