Param ValidateSet with spaces in values

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 1 year, 11 months ago.

  • Author
    Posts
  • #23987
    Profile photo of Brian Britt
    Brian Britt
    Participant

    I have written a script in which I realized that ne of the parameters may be too confusing for the end user type. So I wanted to validate the set and supply the valid values to be used. The values have spaces in them and when I validate the set I am given the choices as I would expect when running the script. But, since there are spaces in the value, the script errors out. I want to include the quotes ("some value") in the values of the validates parameter set but cannot figure how to do it. I have tried double and single quoting. I can get the value to show up as a quoted value but then I get the error that the value supplied is not part of the set.

    param [
    [Parameter(Mandatory=$true)]
    [Validateset('some value','Another value','this is the third value')]
    [string]$configstring
    ]

    This produces an unquoted value when using the script.

    When I try inserting double quotes within the single quote...

    param (
    [Parameter(Mandatory=$true)]
    [Validateset('"some value"','"Another value"','"this is the third value"')]
    [string]$configstring
    )

    I can get the quoted text to show for the script parameter but it errors out with "the argument XXXXXX does not belong to the set"

    How can I get arguments with spaces to correctly show the quotes when using validateset? I have even tried escaping but cant seem to figure it out.

    Brian

  • #23989
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That's a bit of a blind spot in PowerShell's current tab completion logic. However, this question came up recently in another thread, and Rohn Edwards came up with a pretty cool solution: https://powershell.org/forums/topic/dynamic-parameter-question/

You must be logged in to reply to this topic.