Quantcast
Channel: VMware Communities: Message List
Viewing all articles
Browse latest Browse all 231440

Re: Param Validate Script error

$
0
0

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).

 

functionTest-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


Viewing all articles
Browse latest Browse all 231440

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>