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

lf5gs5x2  于 2023-08-05  发布在  Shell
关注(0)|答案(2)|浏览(121)

假设我有一个函数,如:

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

        [Parameter(ParameterSetName='ParamSet1')]
        [string]$Username,

        [Parameter(ParameterSetName='ParamSet1')]
        [string]$Password
    )
  ..
}

字符串
我想执行这些规则:

  • 如果$AuthenticationType = 'UsernameAndPassword',则将参数集ParamSet1设为必填项
  • 如果$AuthenticationType = 'WindowsAuthentication'完全“禁用”ParamSet1

这可能吗?

u59ebvdq

u59ebvdq1#

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

  ...
}


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

yk9xbfzb

yk9xbfzb2#

您可以使用DynamicParam来完成此操作。我最近在here上看到了一个不错的帖子。

DynamicParam {
    if ($AuthenticationType -eq 'UsernameAndPassword') {
        #create ParameterAttribute Objects for the username and password
        $unAttribute = New-Object System.Management.Automation.ParameterAttribute
        $unAttribute.Mandatory = $true
        $unAttribute.HelpMessage = "Please enter your username:"
        $pwAttribute = New-Object System.Management.Automation.ParameterAttribute
        $pwAttribute.Mandatory = $true
        $pwAttribute.HelpMessage = "Please enter a password:"

        #create an attributecollection object for the attributes we just created.
        $attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]

        #add our custom attributes
        $attributeCollection.Add($unAttribute)
        $attributeCollection.Add($pwAttribute)

        #add our paramater specifying the attribute collection
        $unParam = New-Object System.Management.Automation.RuntimeDefinedParameter('username', [string], $attributeCollection)
        $pwParam = New-Object System.Management.Automation.RuntimeDefinedParameter('password', [string], $attributeCollection)

        #expose the name of our parameter
        $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $paramDictionary.Add('username', $unParam)
        $paramDictionary.Add('password', $pwParam)
        return $paramDictionary
    }
}

Process {
    $PSBoundParameters.username
    $PSBoundParameters.password
    }

字符串

相关问题