PowerShell for Windows Admins

Jun 4 2014   1:18PM GMT

Function parameter validation–ValidateScript

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Sometimes you need a little bit more than the standard parameter validation options can provide – in that case use ValidateScript – you can write your own validation routine. For example:

function test {
[CmdletBinding()]
param (
[parameter(Mandatory=$true,ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-Path -Path $_})]
[string]$Path
)

$path

}

All I’ve done to the function used last time is that I’ve added

[ValidateScript({Test-Path -Path $_})]

This will test the path you input to determine if it exists.

Supply a path

£> test -Path C:\Test
C:\Test

and the function works as expected.

£> test -Path
test : Missing an argument for parameter ‘Path’. Specify a parameter of type ‘System.String’ and try again.
At line:1 char:6
+ test -Path
+      ~~~~~
+ CategoryInfo          : InvalidArgument: (:) [test], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,test
Miss off the value of –path and you get the error telling you that the argument is missing. Being able to have multiple validation tests does give you the most flexibility.

£> test -Path C:\Test22
test : Cannot validate argument on parameter ‘Path’. The “Test-Path -Path $_” validation script for the argument with
value “C:\Test22″ did not return a result of True. Determine why the validation script failed, and then try the command
again.
At line:1 char:12
+ test -Path C:\Test22
+            ~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [test], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,test
In this case an invalid path is supplied and the validation script runs and determines that the path is invalid.

In case you were wondering about the order of validation – in this case it doesn’t matter if you change the order

function test {
[CmdletBinding()]
param (
[parameter(Mandatory=$true,ValueFromPipeLine=$true,ValueFromPipeLineByPropertyName=$true)]
[ValidateScript({Test-Path -Path $_})]
[ValidateNotNullOrEmpty()]
[string]$Path
)

$path

}

£> test -Path C:\Test
C:\Test

£> test -Path
test : Missing an argument for parameter ‘Path’. Specify a parameter of type ‘System.String’ and try again.
At line:1 char:6
+ test -Path
+      ~~~~~
+ CategoryInfo          : InvalidArgument: (:) [test], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,test
£> test -Path C:\Test22
test : Cannot validate argument on parameter ‘Path’. The “Test-Path -Path $_” validation script for the argument with
value “C:\Test22″ did not return a result of True. Determine why the validation script failed, and then try the command
again.
At line:1 char:12
+ test -Path C:\Test22
+            ~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [test], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,test
The validation tests fail as expected.

 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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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: