Function fall down if it have got the array with empty string

Welcome Forums General PowerShell Q&A Function fall down if it have got the array with empty string

Viewing 1 reply thread
  • Author
    Posts
    • #196703
      Participant
      Topics: 9
      Replies: 8
      Points: 102
      Rank: Participant

      Hi!
      Strange thing.

      I have the next function:

      So, if I try to execute the next code:

      I’ve got the error:
      Cannot bind argument to parameter 'array' because it is an empty string

      But if, I specify the AllowEmptyString() like this:

      all works fine.

      So, I tried to send the whole array to function, why does only one empty array element cause the error above ?

      Could somebody explain this behavior

    • #196724
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      The validation is occurring on the parameter, not within the function. The validation is ensuring that there is no empty values in the array and if you wish to override the default behavior you can provide the [AllowEmptyString()]. The parameter is being validated because internally the intention is to do a for loop to process each item, such as a computername which would break because it’s null. If you do allow empty strings, you still have to handle the null value in your code. Parameter validation is basically trying to prevent issues later, so it is validating it’s ready to be processed by the function.

Viewing 1 reply thread
  • The topic ‘Function fall down if it have got the array with empty string’ is closed to new replies.