Function parameter validation–ValidateScript

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.

This entry was posted in Powershell Basics. Bookmark the permalink.

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