DynamicParameter Switch 读起来不像静态参数 [switch] 吗?

Does a DynamicParameter Switch not read like a Static Parameter [switch]?

希望标题足够清楚,但是,与静态(类型转换) 开关。

在下面的代码块中,有 2 个 开关 可用 如果其他 2 个参数不为空,and/or,不为空:

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

运行:

returns空。

不幸的是,无论开关是否存在,$Add.IsPresent 都不会计算为任何 布尔值 值。然而在这个函数中它是(这很有意义):

Function Test-StaticParam {
    Param (

        [switch]$Add

    )

    $Add.IsPresent

}

运行:

returns True.

问题

我如何根据所选的动态参数进行评估?

使用$PSBoundParameters自动变量:

Process {    
    $PSBoundParameters['Add'].IsPresent 
}