Validating Input Type

The -as parameter is not widely known but is extremely versatile. It tries to convert data into a .NET type, and when it fails, it simply returns $null. This way, you can create your own functions to test data types. For example, you should use the following Test-Numeric function to validate whether someone has entered a numeric value:

function Test-Numeric($test) {
($test -as [Double]) -ne $null
}

Twitter This Tip! ReTweet this Tip!


Posted Dec 30 2009, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.