PowerShell Error Correction Functions

One of the more annoying things that you have to deal with in building scripts that accept input from users is error correction. Error correction ensures that users who are using a script that requires some input from them (the location of a file, a menu with numbered selections they have to choose from, etc.) can’t input incorrect information and get confused by the weird errors that PowerShell throws out. The reason this is such a pain is that it is very difficult to predict how someone can screw things up on your script. I can’t really give too much of a guide on how to predict this type of thing, since it depends heavily on what you’re doing with your scripts, but I can provide the world with a few canned functions that I created for a giant PowerShell based UI that I created for a client that allowed their non-PowerShell proficient admins to perform PowerShell only functions in Exchange. Use them as you wish by copying and pasting them into your own PowerShell scripts, and calling them appropriately in your code. Also note that some of these functions use other functions. If you want to use them, I recommend adding all of these functions to your script before doing so.

Checknull Function

This function checks input to make sure there is actually some stuff entered. You use the function by placing it in a variable. It would look something like this:

$value = checknull “Enter a number” “You didn’t enter anything. Try again”

This would cause a prompt to show up that says “Enter a Number.” If you hit enter without doing anything, it will fail, display “You didn’t enter anything. Try again” and re-prompt.

And here’s the function:

function checknull($prompt,$errormsg)
{
do{
$err = 0
$input = read-host “$prompt”
if ($input -like $null)
{
write-host “$errormsg”
$err = 1
}
}while ($err -eq 1)
return $input
}

IsNumeric function

This function checks if data that is input is a number or a string. You can occasionally break a PowerShell script that requires users to input numbers by using letters instead. This function checks an existing variable to determine whether it’s a number or a string of letters or a combination of both. It will only return a $true result if the variable is completely numeric. Using this is a bit tricky. But as an example, here is what you can do, in combination with the checknull function:

$value = checknull “Enter a number” “You didn’t enter anything. Try again”

$numeric = isnumeric $value
if($numeric -like $true)
{“Yep. That’s a number”}
else
{“That’s not a number”}

You can run that if you like and see what happens.

Here’s the function:

function isNumeric ($x)
{
try
{
0 + $x | Out-Null
return $true
}
catch
{
return $false
}
}

Checknum Function

This function checks to make sure an entered number is between a specific range as defined in the function call. You use this by entering it into a variable, just like the checknull function. It uses the isNumeric function to ensure that entered data is actually a number and not a letter. So it’s nested error correction (hurray). You use it like this if you wanted a user to enter a number between 1 and 5

$number = checknum “Enter a number between 1 and 5” 1 5

It will automatically re-prompt if the user enters something outside the range. I don’t think it will handle negative numbers, but you are welcome to try. Here’s the function.

function checknum ($prompt,$min,$max)
{
do{
$err = 0
$input = read-host $prompt -erroraction silentlycontinue
$num = isnumeric $input
if (($input -lt $min) -or ($input -gt $max) -or ($num -like $false))
{
write-host “Please enter a number between $min and $max”
$err = 1
}
}while ($err -eq 1)
return $input
}

CheckMailbox function

This function checks to see if a mailbox in Exchange 2007+ exists. You can use this to make sure a mailbox exists before performing some bulk action, or you can just use it as is to see if a mailbox exists. This is also a nested error correction function. To use it, simply enter checkmailbox “Enter a mailbox name”. Note that this function is not compatible with bulk operations. It can only handle one mailbox at a time. It will re-prompt the user until they enter a valid mailbox.

function checkmailbox ($prompt)
{
do {
$err = 0
$mailbox= checknull $prompt
#Check Moderator name entered for validity
try
{
get-mailbox $mailbox -erroraction stop | out-null
}
catch
{
write-host “$mailbox does not exist”
$err = 1
}
}while ($err -eq 1)
return $mailbox
}

CheckFile function

This function checks to see if a file path exists. Pretty self explanatory. Same rules as the checkmailbox function, same usage.

function checkfile ($prompt)
{
do{
$file = checknull “$prompt” “Please enter a valid File Name”
try
{
$err = 0
dir $file -erroraction stop | out-null
}
catch
{
write-host “$file does not exist.”
$err = 1
}
}while ($err -eq 1)
return $file
}

And that’s what I have so far. Feel free to comment and add your own error correction functions if you wish or link to your blog that contains them.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s