4
            PARAM (
                [parameter(Mandatory=$true)]
                [string]$Poolname,
                [array]$Ports = 443,
                [parameter(Mandatory=$true)]
                [ValidateSet("ELB","ALB")]
                $Loadbalncertype,
                [parameter(Mandatory=$true)]
                [ValidateSet("Ping","HTTPGet")]
                $HealthCheckConfigType,
                [parameter(Mandatory=$true)]
                [array]$LBSubnets,
            [parameter(Mandatory=$true)]
            [string]$SecGroupID,
            [int]$IdleTimeoutsec = 60,
            [bool]$SSLPassthrough = $false,
            string]$SSLCertificateName,
                [string]$HealthCheckPath,
            [string]$SSLPolicyName,
                [bool]$ConfigureProxyProtocol = $true
                )

In the above I would like to use Parameter $HealthCheckConfigType only if the $Loadbalncertype = ELB. I am not sure how to create this logic in Powershell function parameter section.

0

2 Answers 2

3

To do that in the param definition specifically, you could use DynamicParam to create a dynamic parameter, but that's a lot of work and probably overkill.

The most straightforward method I can think of if you must leave $LoadBalancerType as a [string] is to use [ValidateScript()] like so:

param(
    [ValidateSet("ELB","ALB")]
    $LoadBalancerType ,

    [ValidateScript( { $LoadBalancerType -eq 'ELB' } )]
    [ValidateSet("Ping","HTTPGet")]
    $HealthCheckConfigType
)

This will give a crappy error message, which you could override with a well-placed throw:

[ValidateScript( { $LoadBalancerType -eq 'ELB' -or $(throw 'A better error message') } )]

Another option is to change your $LoadBalancerType parameter into separate switch parameters, and use them to define parameter sets:

[CmdletBinding(DefaultParameterSet='ALB')]
param(
    [parameter(Mandatory=$true)]
    [string]$Poolname,

    [Parameter(Mandatory, ParameterSetName = 'ALB')]
    [Switch]$ALB ,

    [Parameter(Mandatory, ParameterSetName = 'ELB')]
    [Switch]$ELB ,

    [Parameter(Mandatory, ParameterSetName = 'ELB')
    [ValidateSet("Ping","HTTPGet")]
    $HealthCheckConfigType
)

This lets the parameter parser enforce this restriction, and you can see it in the automatically generated parameter sets by calling Get-Help on your function.

And, even though this isn't the usual way, in your case if you name the parameter sets with the names of the values you wanted, you could recreate $LoadBalancerType without conditionals:

$LoadBalancerType = $PSCmdlet.ParameterSetName

(assuming of course, that the only possible parameter sets are load balancer names directly; be careful with this)

But if you never really needed that string value; i.e. if you were only ever going to do:

if ($LoadBalancerType -eq 'ALB') {

} elseif ($LoadBalancerType -eq 'ELB') {

}

or something like that, then you don't need to recreate it, just do:

if ($ALB) {

} elseif ($ELB) {

}

Alternatively, you don't have to do this check in the param block at all; you can do it in your function body, or begin/process blocks where appropriate.

Sign up to request clarification or add additional context in comments.

10 Comments

Does ValidateScript work that way? I tried this earlier today for a function I was writing and I got some strangeness trying to access another parameter variable from the ValidateScript block.
@BendertheGreatest you know, I thought I remembered having issues with it too... but before I wrote the answer I made a quick POC and it seemed to work as intended.
Conversely, I rewrote my ValidateScript block to operate on other parameters and it's simply not working. I don't use this attribute enough though to know if I'm wrong or you are :D
@BendertheGreatest oh very interesting. What's your $PSVersionTable look like? And how about your function definition? Here's mine: function ttt { [CmdletBinding()]param([ValidateSet('a','b')]$a, [ValidateScript( {$a -eq 'b'})]$b) "$a~$b" } If I invoke ttt -a b -b whatever it works, if I do ttt -a a -b whatever it throws. If I leave off -b then either value of -a works. Does this simple example work for you?
Running 5.1, I'm up to date. Here's what I'm doing (with params only, don't have a body yet): Param( [int]$Capacity, [ValidateScript({ ( $Capacity -And $_.Capacity -le $Capacity ) -Or -Not $Capacity })] [Parameter(ValueFromPipeline)] [System.Collections.IEnumerable]$IEnumerable ) Basically if a passed-in List capacity is greater than specified capacity it should fail to validate. But this isn't the case.
|
2

Use a DynamicParam block: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6#dynamic-parameters

I don't have a ton of experience with them myself, but here is the code sample from that MS doc. You can process and add any parameters you need:

  function Get-Sample {
  [CmdletBinding()]
  Param ([String]$Name, [String]$Path)

  DynamicParam
  {
    if ($path -match ".HKLM.:")
    {
      $attributes = New-Object -Type `
        System.Management.Automation.ParameterAttribute
      $attributes.ParameterSetName = "__AllParameterSets"
      $attributes.Mandatory = $false
      $attributeCollection = New-Object `
        -Type System.Collections.ObjectModel.Collection[System.Attribute]
      $attributeCollection.Add($attributes)

      $dynParam1 = New-Object -Type `
        System.Management.Automation.RuntimeDefinedParameter("dp1", [Int32],
          $attributeCollection)

      $paramDictionary = New-Object `
        -Type System.Management.Automation.RuntimeDefinedParameterDictionary
      $paramDictionary.Add("dp1", $dynParam1)
      return $paramDictionary
    }
  }
}

Another option would be to not muck with DynamicParam and in your code body, simply ignore $HealthCheckConfigType if $LoadBalancerType does not equal ELB, but if you want to use parameter validators to check this, DynamicParam is the answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.