none
Using Spaces in ValiadateSet() attribute RRS feed

  • Question

  • Hi,

    I am learning how to create functions and I was trying to create a function which has a parameter with a validate set where there are white space.

    For example

            [Parameter(
             HelpMessage = "Enter a service state here",
             ValuefromPipeline = $False,
             ValueFromPipelineByPropertyName = $False
             )]
            [ValidateSet("Stopped",'"Start Pending"',"'Stop Pending'","Running","'Continue Pending'","'Pause Pending'","Paused","Unknown")]
            [ValidateNotNullOrEmpty()]
            [String]$ServiceState = 'Stopped'

    But I am getting error as below. I have tried using looped quotations. But cant get around this logic.

    TAB completion in console doesnt wrap it up with quotes. 

    Any help on this would be great to help me progress :)

    Get-MyServicesInformation : Cannot validate argument on parameter 'ServiceState'. The argument "Start Pending" does
    not belong to the set "Stopped,"Start Pending",'Stop Pending',Running,'Continue Pending','Pause
    Pending',Paused,Unknown" specified by the ValidateSet attribute. Supply an argument that is in the set and then try
    the command again.
    At line:1 char:97
    + ...  -ServiceState "Start Pending"
    +                    ~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-MyServicesInformation], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-MyServicesInformation


    If you find this helpful, kindly mark as answer. If you have any queries, please post back as a reply. Will look forward to your feedback. Thanking You Soumyajyoti Biswas

    Sunday, May 15, 2016 1:10 PM

Answers

  • It only works if you type the command correctly.

    test -servicestate <tab>

    The earlier versions do not support spaces.  I think this is a known bug or deficiency.


    \_(ツ)_/


    • Proposed as answer by jrv Sunday, May 15, 2016 3:11 PM
    • Unproposed as answer by jrv Sunday, May 15, 2016 3:11 PM
    • Edited by jrv Sunday, May 15, 2016 3:12 PM
    • Marked as answer by Soumyajyoti.Biswas Thursday, May 19, 2016 10:55 AM
    Sunday, May 15, 2016 3:08 PM

All replies

  • Why so many quotes? 

    Don't include parameter attributes that are false.

    function test{
    	Param(
    		[Parameter(HelpMessage = 'Enter a service state here')]
            	[ValidateSet("Stopped",'Start Pending','Stop Pending','Running','Continue Pending','Pause Pending','Paused','Unknown')]
            	[ValidateNotNullOrEmpty()]
            	[string]$ServiceState = 'Stopped'
    	)
    	
    	$PSBoundParameters
    
    }


    \_(ツ)_/

    Sunday, May 15, 2016 2:26 PM
  • Hello,

    Thank you for the reply JRV :):)

    I used single quotes too and checked. But when I tab complete the value of the parameter in console, it is not automatically filling in quotes where there are white spaces. I checked in v2,v3 and v4.

    Surprisingly in v5 this works.

    I have put the snippets below. :(

    I know filling in false parameters are not needed, but just for time being to complete the understanding


    If you find this helpful, kindly mark as answer. If you have any queries, please post back as a reply. Will look forward to your feedback. Thanking You Soumyajyoti Biswas

    Sunday, May 15, 2016 2:51 PM
  • It only works if you type the command correctly.

    test -servicestate <tab>

    The earlier versions do not support spaces.  I think this is a known bug or deficiency.


    \_(ツ)_/


    • Proposed as answer by jrv Sunday, May 15, 2016 3:11 PM
    • Unproposed as answer by jrv Sunday, May 15, 2016 3:11 PM
    • Edited by jrv Sunday, May 15, 2016 3:12 PM
    • Marked as answer by Soumyajyoti.Biswas Thursday, May 19, 2016 10:55 AM
    Sunday, May 15, 2016 3:08 PM
  • Hi,

    Yes, i used the full command also to test. The snippets are just the quick way around them as the parameter being the only one will always be in position 1.

    Is there any way around this bug ? Any good links or study material on it ?

    Thanks for all the help jrv :) :)


    If you find this helpful, kindly mark as answer. If you have any queries, please post back as a reply. Will look forward to your feedback. Thanking You Soumyajyoti Biswas

    Sunday, May 15, 2016 3:24 PM
  • I have not more information.  I suggest that using spaces may not be a good idea and is likely unnecessary.

    \_(ツ)_/

    Sunday, May 15, 2016 3:40 PM