2

I am confused about how to do error handling in the case that a mandatory variable remains null.

function parse-com{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$True)]
        [string[]]$list
     )
...

In this case if no argument is passed for $list then I am prompted for it, but if I just hit enter (pass a null to $list) then I throw an error. What I would rather do is throw a usage statement and/or exit gracefully. Example...

PS C:\Users\memyself> parse-com
cmdlet parse-com at command pipeline position 1
Supply values for the following parameters:
list[0]: 
parse-com : Cannot bind argument to parameter 'list' because it is an empty array.
At line:1 char:1
+ parse-com
+ ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [parse-com], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,parse-com
1
  • 1
    Look into using a ValidateScript on the parameter instead of merely making it mandatory. Commented Jun 9, 2017 at 18:13

1 Answer 1

5

When passing a $null value, that satisfies the Mandatory requirement. If you want the command then to fail prior to execution then you should use validation as @JeffZeitlin suggests.

It sounds like what you need to validate is that the value is not $null nor is it an empty array. For which you could user [ValidateNotNullOrEmpty()]

function parse-com{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [ValidateNotNullOrEmpty()]
        [string[]]$list
    )
Sign up to request clarification or add additional context in comments.

5 Comments

I had high hopes, but I cannot really see that it does anything other than throw a different error.
PS D:\MyCOMTracker> D:\MyCOMTracker\parse-com.ps1 cmdlet parse-com.ps1 at command pipeline position 1 Supply values for the following parameters: list[0]: D:\MyCOMTracker\parse-com.ps1 : Cannot validate argument on parameter 'list'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again. + CategoryInfo : InvalidData: (:) [parse-com.ps1], ParentContainsErrorRecordException + FullyQualifiedErrorId : ParameterArgumentValidationError,parse-com.ps1
@klaypigeon The point of the Validate is to throw that error before the command executes. If you want to just accept a null value, then take out the mandatory and the validate and you'll have to write your own logic around what to do when the parameter is $null inside the function.
This answer is wrong and misleading, as Mandatory already implies not $null or empty. That's why the options AllowNull or AllowEmptyString exist for Mandatory parameters. See their description in the docs: learn.microsoft.com/en-us/powershell/module/…. Solving OPs problem needs a different streak, like e.g. making the parameter non-mandatory or check from the calling site.
But ValidateNotNullOrEmpty can be used to check untyped parameters or parameters which accept $null. See: learn.microsoft.com/en-us/powershell/module/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.