powershell - 我可以使参数集依赖于另一个参数的值吗?

标签 powershell

假设我有一个函数:

function Authenticate
{
  param
  (
    [ValidateSet('WindowsAuthentication','UsernameAndPassword')][string] $AuthenticationType,
    [Parameter(ParameterSetName='Set1')][string] $Username,
    [Parameter(ParameterSetName='Set1')][string] $Password
  )
  ..
}

而且我想在 $AuthenticationType = 'UsernameAndPassword' 时将参数设置为强制性的,但如果 $AuthenticationType = 'WindowsAuthentication' 也不能使用它>.

在 PowerShell 中这甚至可能吗?

最佳答案

使用 Tim Ferrill 的回答中的链接,我创建了以下函数来帮助创建动态参数:

function New-DynamicParameter
{
    [CmdletBinding(DefaultParameterSetName = 'Core')]    
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)][string] $Name,
        [Parameter(Mandatory = $true, ParameterSetName = 'Core')][Parameter(Mandatory = $true, ParameterSetName = 'ValidateSet')][type] $Type,
        [Parameter(Mandatory = $false)][string] $ParameterSetName = '__AllParameterSets',
        [Parameter(Mandatory = $false)][bool] $Mandatory = $false,
        [Parameter(Mandatory = $false)][int] $Position,
        [Parameter(Mandatory = $false)][bool] $ValueFromPipelineByPropertyName = $false,
        [Parameter(Mandatory = $false)][string] $HelpMessage,
        [Parameter(Mandatory = $true, ParameterSetName = 'ValidateSet')][string[]] $ValidateSet,
        [Parameter(Mandatory = $false, ParameterSetName = 'ValidateSet')][bool] $IgnoreCase = $true
    )

    process
    {
        # Define Parameter Attributes
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.ParameterSetName = $ParameterSetName
        $ParameterAttribute.Mandatory = $Mandatory
        $ParameterAttribute.Position = $Position
        $ParameterAttribute.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName
        $ParameterAttribute.HelpMessage = $HelpMessage

        # Define Parameter Validation Options if ValidateSet set was used
        if ($PSCmdlet.ParameterSetName -eq 'ValidateSet')
        {
            $ParameterValidateSet = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet -Strict (!$IgnoreCase)
        }

        # Add Parameter Attributes and ValidateSet to an Attribute Collection
        $AttributeCollection = New-Object Collections.ObjectModel.Collection[System.Attribute]
        $AttributeCollection.Add($ParameterAttribute)
        $AttributeCollection.Add($ParameterValidateSet)

        # Add parameter to parameter list
        $Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter -ArgumentList @($Name, $Type, $AttributeCollection)

        # Expose parameter to the namespace
        $ParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $ParameterDictionary.Add($Name, $Parameter)
        return $ParameterDictionary
    }
}

并通过以下方式解决了我的特定问题:

function Authenticate
{
  param
  (
    [ValidateSet('WindowsAuthentication','UsernameAndPassword')][string] $AuthenticationType,
  )

  DynamicParam
  {
    if ($AuthenticationType -eq 'UsernameAndPassword')
    {
      New-DynamicParameter Username [string] -Mandatory $true
      New-DynamicParameter Password [string] -Mandatory $true
    }
  }

  ...
}

使用动态参数时不需要设置参数,所以我删除了参数集。

关于powershell - 我可以使参数集依赖于另一个参数的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24234176/

相关文章:

powershell - 从 CSV 文件设置网络接口(interface)卡的 Azure DN 设置

powershell - 如何在PowerShell中读取ATOM XML

powershell - 运行 Start-DscConfiguration 给出 "the network connection to computername has been interrupted"

windows - 使用 PowerShell 在计算机上查找特定的 .exe 文件

powershell - 将具有特定扩展名的文件 move 到更高层次的文件夹中

使用 schtasks.exe 的 Powershell 计划任务返回错误 无法识别成功

c# - 代表所有的 PowerShell 枚举值

powershell 使用 Sort-Object 按值对 json 进行排序

java - 无法从命令行运行 JUnit 测试用例

file - Powershell:检查文件是否被锁定