0

Hopefully the Title is clear enough but, I am having some trouble understanding on how to evaluate against a DynamicParameter Switch, compared to a Static (type casted) switch.

In the following code block, there are 2 switches that become available only if the other 2 parameters are not null, and/or, are not empty:

  • Add
  • Remove
Function Test-DynamParam {
    Param (
        # Input Parameters
        [Parameter(Mandatory = $false,
                   HelpMessage='Enter. Workflow. Name.')]
        [Alias('OMB','MailBox')]
        [string]$Workflow,

        [Parameter(Mandatory = $false)]
        [Alias('EDIPI','DisplayName')]
        [string[]]$UserName

    )

      DynamicParam {
        if ($Workflow -ne $null -and $UserName -ne $null) {
          $parameterAttribute = [System.Management.Automation.ParameterAttribute]@{
              ParameterSetName = "AddingMembers"
              Mandatory = $false
          }

          $attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
          $attributeCollection.Add($parameterAttribute)

          $dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new(
            'Add', [switch], $attributeCollection
          )

          $paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
          $paramDictionary.Add('Add', $dynParam1)
          

          $parameterAttribute1 = [System.Management.Automation.ParameterAttribute]@{
              ParameterSetName = "RemovingMembers"
              Mandatory = $false
          }

          $attributeCollection1 = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
          $attributeCollection1.Add($parameterAttribute1)

          $dynParam11 = [System.Management.Automation.RuntimeDefinedParameter]::new(
            'Remove', [switch], $attributeCollection1
          )

          $paramDictionary.Add('Remove', $dynParam11)
          return $paramDictionary

        }
      }

    Process {    
        $Add.IsPresent 
    }
}

Running:

  • Test-DynamParam -Workflow 'd' -UserName 'a' -Add

returns empty.

Unfortunately, $Add.IsPresent is not evaluated to any boolean value regardless if the switch is present or not. Yet in this function it is (which makes sense):

Function Test-StaticParam {
    Param (

        [switch]$Add

    )

    $Add.IsPresent

}

Running:

  • Test-StaticParam -Add

returns True.

Question

How can I evaluate against dynamic parameter chosen?

Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24

1 Answers1

2

Use the $PSBoundParameters automatic variable:

Process {    
    $PSBoundParameters['Add'].IsPresent 
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Glad smart people are up early! This worked for me. Thank ya! Also, wasn't aware that was a function of `$PSBoundParameters`; was using it like this instead: `$PSBoundParameters.ContainsKey('Add')` which didn't give me no result. Will accept the answer when it allows me to. – Abraham Zinala Sep 01 '21 at 14:33
  • 1
    @AbrahamZinala The nice thing about `$PSBoundParameters` is it implements `IDictionary` - so you can treat it like any other dictionary/hashtable :-) – Mathias R. Jessen Sep 01 '21 at 15:15