16

I have a problem similar to THIS ONE

I'm passing to a function 3 arrays and I validate object type this way

function _TEST {
[CmdletBinding()]
param (
    [parameter(mandatory=$true)]
    [array]$Path,
    [parameter(mandatory=$true)]
    [array]$RW,
    [parameter(mandatory=$true)]
    [array]$RO
)
process {
    # my code
}

It works unless I pass to function array without elements, in that case it returns this error _TEST : Cannot bind argument to parameter 'Path' because it is an empty collection.

Is there a way to solve the problem similar to [AllowEmptyString()] in linked question or do I have to write custom code to test input variable?

1 Answer 1

38

Try this:

param (
    [parameter(mandatory=$true)]
    [AllowEmptyCollection()]
    [array]$Path
)

Link:

Parameter Validation Attributes

Sign up to request clarification or add additional context in comments.

1 Comment

Using this code it allows also objects different from array: _TEST @() --> no errors ; _TEST "string" --> no errors

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.