PowerShell for Windows Admins

Jul 8 2013   1:39PM GMT

Validating parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

PowerShell provides a number of ways of validating the values input to the parameters in your scripts. One such way is Validate script. I came across an interesting question where the inquirer wanted to test an input parameter and throw an error if the 4th character was a “T”

One solution is:

function ptest {
[CmdletBinding()]
param(
[ValidateScript({$_.Substring(3,1) -ne “T” })]
[string]$server
)
Write-Host “$server”
}

Alternatively use a regular expression

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: