Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Parameter Validation in a function - How to use with a specific parameter?

$
0
0

The Clever Parameter Validation tip spawned a question for me.

http://powershell.com/cs/blogs/tips/archive/2015/04/02/clever-parameter-validation.aspx

How do I use this for a specific parameter, inside a function?

The parameter should have two possible inputs, and a variable is defined based on the input that is chosen. How do I validate that selection for the specific parameter inside the function.

Here is a sample of what I'm working with and how I guessed it would work.

[CmdletBinding()]

Param (

[Parameter(Mandatory = $true,Position=1)]

[String]$Username,

 

[Parameter(Mandatory = $false,Position=1)]

[ValidateSet('Option1','Option')]

[String]$Location

)

If ($Location -eq 'Option1')

{

    $db = "DB01"

}

else

    {

    If ($Location -eq 'Option2')

        {

            $db = "DB02"

        {

        else

        {

            Write-Host "Please select Option1 or Option2"

        }

    }

 

Am I on the right track or am I way off?


Viewing all articles
Browse latest Browse all 6937

Trending Articles