VMware Cloud Community
nlong12
Enthusiast
Enthusiast
Jump to solution

Param Validate Script error

Greetings experts

Trying to figure out how to get validatescript to work.   Currently it allows incorrect datacenter name and it is allowing me to enter multiple datacenter names, only need to enter only one datacenter name.

Any help would be greatly appreciated

Norm

#cmdlet binding example

[CmdletBinding()]

param(

    [Parameter(Mandatory,HelpMessage="Enter a Datacenter Name")]

     

[ValidateScript(

       

            {

                 (Get-Datacenter $DCChoice -ErrorAction SilentlyContinue)

                    throw "Please enter a valid Datacenter Name."

               

                $true

            }

    )]

    [String[]]

    $DCChoice

    )

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In the validation script the value of the variable is passed as in a pipeline.
You have to use $_

This test you want to perform is ideal for a try-catch setup.

A validation script has to return $true or $false.
But any object, also a Datacenter object, besides $null is considered $true, so you can just use the result of the Get-Datacenter.

With the catch block, nothing, or $null, is returned.

Hence the validation fails.

I normally test this with a short test function, to which I then feed all possible combinations.

A short example (adapt the values on the parameter according to your environment).

function Test-Validation

{

[CmdletBinding()]

param(

    [Parameter(Mandatory,HelpMessage="Enter a Datacenter Name")]

    [ValidateScript({

        try{

            Get-DataCenter -Name $_ -ErrorAction Stop 

        }

        catch{

            throw "Datacenter $_ not found. Please enter a valid Datacenter Name."

        }

    })]

    [String[]]

    $DCChoice

    )


    $DCChoice

}


Test-Validation -DCChoice validDC

Test-Validation -DCChoice invalidDC

Test-Validation -DCChoice validDC,validDC

Test-Validation -DCChoice validDC,invaliddc

Test-Validation -DCChoice invaliddc,invaliddc


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

In the validation script the value of the variable is passed as in a pipeline.
You have to use $_

This test you want to perform is ideal for a try-catch setup.

A validation script has to return $true or $false.
But any object, also a Datacenter object, besides $null is considered $true, so you can just use the result of the Get-Datacenter.

With the catch block, nothing, or $null, is returned.

Hence the validation fails.

I normally test this with a short test function, to which I then feed all possible combinations.

A short example (adapt the values on the parameter according to your environment).

function Test-Validation

{

[CmdletBinding()]

param(

    [Parameter(Mandatory,HelpMessage="Enter a Datacenter Name")]

    [ValidateScript({

        try{

            Get-DataCenter -Name $_ -ErrorAction Stop 

        }

        catch{

            throw "Datacenter $_ not found. Please enter a valid Datacenter Name."

        }

    })]

    [String[]]

    $DCChoice

    )


    $DCChoice

}


Test-Validation -DCChoice validDC

Test-Validation -DCChoice invalidDC

Test-Validation -DCChoice validDC,validDC

Test-Validation -DCChoice validDC,invaliddc

Test-Validation -DCChoice invaliddc,invaliddc


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nlong12
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Thank you for your response, sadly the test with a valid datacenter did not work:

PS> Test-Validation -DCChoice WVHQ

Test-Validation : Cannot validate argument on parameter 'DCChoice'. The "

        try{

            Get-DataCenter -Name $_ -ErrorAction Stop | out-null  

        }

        catch{

            throw "Datacenter $_ not found. Please enter a valid Datacenter Name."

        }

    " validation script for the argument with value "WVHQ" did not return a result of True. Determine why the validation script failed, and then try the command again.

At line:1 char:27

+ Test-Validation -DCChoice WVHQ

+                           ~~~~

    + CategoryInfo          : InvalidData: (:) [Test-Validation], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Validation

Thoughts?

Thank you for your help

Norm

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That Out-Null shouldn't have been in there (was a relic from a test I did).
I corrected my code above.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
nlong12
Enthusiast
Enthusiast
Jump to solution

LucD,

Works now!!   As always thank you!!

Norm

0 Kudos