PowerShell for Windows Admins

Jun 3 2014   11:57AM GMT

Function parameter validation–NotNullorEmpty

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A question on the forum made me realise that there is still  a lot of confusion around the advanced function parameter validation options so I thought I’d spend  a few posts clearing some of the confusion.

The question was about ValidateNotNullorEmpty so I’ll start with that one.

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

$path

}

If you supply a path – it’s echoed back to you:

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

If you don’t supply the path parameter you’ll be prompted for it:

£> test
cmdlet test at command pipeline position 1
Supply values for the following parameters:
Path: c:\test
c:\test

if you use the function and don’t supply a value:

£> 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

The validation routine kicks in.

The pipeline works as well:

£> Get-ChildItem -Path c:\ -Filter test | test
Test

Though notice that its returning Test instead of c:\test – that’s because Get-ChildItem doesn’t return a property called path but Name is the first string property accessible in the object which is why its used.

Now if you try to access a non-existent path in this way

£> Get-ChildItem -Path c:\ -Filter test27 | test
£>

You’ll get nothing returned  – because

Get-ChildItem -Path c:\ -Filter test27

doesn’t put anything on the pipeline so test is never called.

If you want to see the function work with a non-existent folder try this:

£> test -Path (Get-ChildItem -Path c:\ -Filter test27)
test : Cannot validate argument on parameter ‘Path’. The argument is null or empty. Provide an argument that is not null
or empty, and then try the command again.
At line:1 char:12
+ test -Path (Get-ChildItem -Path c:\ -Filter test27)
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [test], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,test

ValidateNotNullEmpty is a good test for determining if a value is present and not Null or an empty string. It should be paired where possible with mandatory=$true to ensure that the parameter is used.

This and other validation options are discussed in about_Functions_Advanced_Parameters

 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: