我可以根据不同参数的_值_将一个参数设置为强制参数吗?

red*_*888 4 parameters powershell function

如果参数一设置为 bbb 那么我想强制参数二。是否可以通过参数集以某种方式执行此操作,或者我只需将此逻辑添加到脚本本身?

\n

我需要的示例想法:

\n
 param (\n        [Parameter(Mandatory, ParameterSetName = 'default')]\n        [ValidateSet(\xe2\x80\x9caaa\xe2\x80\x9d, \xe2\x80\x9dbbb\xe2\x80\x9d, \xe2\x80\x9dccc\xe2\x80\x9d, "ddd")]\n        [String]\n        $One,\n\n        [Parameter(ParameterSetName = 'default')]\n        [ValidateScript( { $One -eq 'bbb'; THEN MAKE THIS PARAM MANDATORY! })]\n        [String]\n        $Two\n    )\n
Run Code Online (Sandbox Code Playgroud)\n

当我尝试这样做并且 one.txt 为空时, $One 的值似乎尚未设置

\n
[ValidateScript( { $One > One.txt; $true })]\n
Run Code Online (Sandbox Code Playgroud)\n

编辑

\n

虽然 DynamicParam{} 看起来只有在您有开始、处理、结束等设置时才有效。这是一个简单的函数,我不想将其添加到其中。此外,DynamicParam 似乎需要大量的样板代码才能工作

\n

编辑

\n

看起来 DynamicParam 是唯一的方法,但我认为这很疯狂。这很奇怪而且不可读,但我仍然更喜欢 Powershell 来为我处理验证。

\n

自己做仍然很简单:

\n
if ($One -eq 'bbb' -and -not $Two) {\n    ThrowError Param Two required when One set to $One\n}\n
Run Code Online (Sandbox Code Playgroud)\n

mkl*_*nt0 5

-Twoif通过使用以下语句的语句,使用参数的默认值来强制执行所需的逻辑Throw

param (
  [Parameter(Mandatory, ParameterSetName = 'default')]
  [ValidateSet('aaa', 'bbb', 'ccc', 'ddd')]
  [String]
  $One,

  [Parameter(ParameterSetName = 'default')]
  [String]
  $Two = $(if ($One -eq 'bbb') { Throw "-Two must be passed if -One equals 'bbb'." })
)

"-One: $One; -Two: $Two"
Run Code Online (Sandbox Code Playgroud)

注意:模拟(有条件)强制参数Throw意味着该行为与常规Mandatory参数不同,因为后者会在未给出值时进行提示。

基于验证属性的解决方案会更好,但验证属性不是为参数验证而设计的,并且不保证其评估的特定顺序。

上述解决方案依赖于这样一个事实:默认值是在显式传递的参数被绑定 后计算的。

详细的替代方案是使用动态参数 ,如Wasif Hasan 的答案所示,依赖于相同的时间,尽管它确实具有表现出正常提示缺少强制值行为的优点。

截至撰写本文时,Wasif 的答案并不像发布的那样有效,因此这里有一个可行的解决方案;请注意 的使用DynamicParambeginprocessend在语法上如何需要(至少其中之一),

# Syntax requires PSv5+:
using namespace System.Management.Automation

param( 
  [Parameter(Mandatory, ParameterSetName='default')]
  [ValidateSet('aaa', 'bbb', 'ccc', 'ddd')]
  [String]$One
)

# Define parameter -Two *dynamically*, so that its Mandatory property
# can be based on the specific value of the already-bound static -One parameter.
DynamicParam {

  # Create a the dictionary of dynamic parameters.
  $dict = [RuntimeDefinedParameterDictionary]::new()

  # Define and add the -Two parameter 
  $paramName = 'Two'
  $dict.Add(
    $paramName,
    [RuntimeDefinedParameter]::new(
      $paramName,
      [string],
      [ParameterAttribute] @{
        ParameterSetName = 'default'
        # *Conditionally* make the parameter mandatory, depending on the value
        # of the already-bound static -One parameter.
        Mandatory = $One -eq 'bbb'
      }
    )
  )

  # Return the dictionary
  return $dict
}

begin {

  # NOTE: Dynamic parameter values do not become local variables the way
  #       they do for static parameters; the value must be accessed via the
  #       automatic $PSBoundParameters dictionary.
  $Two = $PSBoundParameters['Two']

  "-One: $One; -Two: $Two"

}
Run Code Online (Sandbox Code Playgroud)