/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell Celsius Fahrenheit converting "," PowerShell : How Can I Rename Files Using File Nam... "," PowerShell make a drive of an UNC path "," Some Powershell News "," PowerShell Import and Export a DirectoryTree and S... "," PowerShell How Can I Query a Text File and Retriev... "," PowerShell Import Shares and Security info From CSV "," An other PowerShell Blog is born "," Windows PowerShell: TFM "," PowerShell Export Shares and Security info to CSV "," ")

Wednesday, May 17, 2006

 


PowerShell basic parameter checking of a function



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.html

I 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 :


Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?