You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell自定义函数Add/Remove开关交互输入逻辑修复咨询

解决方案

需修改的核心内容

  • 移除原有分离的$ABool、$RBool两个脚本块,合并为单次交互逻辑,仅询问用户一次操作类型,避免输入Remove时误触发Add校验分支的break逻辑直接退出函数
  • 修正动态参数判断逻辑:访问$PSBoundParameters内的开关参数前先判断参数是否存在,避免未传$UserName时动态参数未生成导致的属性访问错误
  • 调整分支判断顺序,仅当-Add和-Remove两个开关都没有传入时才触发交互询问

修改后完整代码

Function Test-Bool {
        
    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

        }
      }      
    Begin {
        # 判断是否传入了开关参数
        if ($PSBoundParameters.ContainsKey('Add') -and $PSBoundParameters['Add'].IsPresent) { 
            [bool]$Add = $true 
        }
        elseif ($PSBoundParameters.ContainsKey('Remove') -and $PSBoundParameters['Remove'].IsPresent) { 
            [bool]$Remove = $true 
        }
        else {
            # 两个开关都未传入时触发交互
            $operateInput = Read-Host -Prompt 'Adding or Removing Member(s)'
            $operateInput = $operateInput.ToLower()
            if ($operateInput -like 'a*') {
                $Add = $true
            }
            elseif ($operateInput -like 'r*') {
                $Remove = $true
            }
            else {
                Write-Warning "无效输入,请输入以A/Add或R/Remove开头的内容"
                return
            }
        }
    }
    Process {
        if ($Add) {       
            "Add was selected"
        }
        if ($Remove) {
            "Remove was selected"
        }
    }
}

效果验证

修改后所有调用场景均可正常工作:

  • 显式传-Add或-Remove开关:正常触发对应流程
  • 未传开关参数触发交互:输入Add则走添加逻辑,输入Remove则走删除逻辑

内容的提问来源于stack exchange,提问作者Abraham Zinala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 05:36:02