Function Powershell条件参数SetName validateset
我知道如何使用ValidateSet和ParameterSetName。我需要根据ValidateSet中先前选择的值动态更改ParameterSetName的值。这可能吗 似乎存在动态参数: 我的用例:Function Powershell条件参数SetName validateset,function,powershell,validation,parameters,Function,Powershell,Validation,Parameters,我知道如何使用ValidateSet和ParameterSetName。我需要根据ValidateSet中先前选择的值动态更改ParameterSetName的值。这可能吗 似乎存在动态参数: 我的用例: param( [Parameter(Mandatory=$true)] [string]$Symbol, [Parameter(Mandatory=$true)] [ValidateSet("BUY", "SELL"
param(
[Parameter(Mandatory=$true)]
[string]$Symbol,
[Parameter(Mandatory=$true)]
[ValidateSet("BUY", "SELL")]
[string]$Side,
[Parameter(Mandatory=$true)]
[ValidateSet("Market", "Limit", "Take limit")]
[string]$OrderType,
[Parameter(Mandatory=$false)]
[string]$Price,
[Parameter(Mandatory=$false)]
[string]$StopPrice,
[Parameter(Mandatory=$false)]
[string]$Quantity,
[Parameter(Mandatory=$false)]
[string]$quoteOrderQty
)
需要许多条件,例如,如果从$OrderType
中选择“Market”,则需要$quoteOrderQty
,然后应是相同参数setname的一部分
希望我足够清楚,如果您有更好的方法,请提供建议。
非常感谢
Yann动态参数是仅在特定条件下可用的cmdlet、函数或脚本的参数。 还可以创建一个参数,该参数仅在函数命令中使用另一个参数或另一个参数具有特定值时显示 仅当Path参数的值以HKLM开头时,Get Sample函数中的DP1参数才可用:
function Get-Sample {
[CmdletBinding()]
Param([String]$Name, [String]$Path)
DynamicParam
{
if ($Path.StartsWith("HKLM:"))
{
$attributes = New-Object -Type `
System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "PSet1"
$attributes.Mandatory = $false
$attributeCollection = New-Object `
-Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
$dynParam1 = New-Object -Type `
System.Management.Automation.RuntimeDefinedParameter("DP1", [Int32],
$attributeCollection)
$paramDictionary = New-Object `
-Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("DP1", $dynParam1)
return $paramDictionary
}
}
}
我不知道
参数setName
有条件选项。如果这是非常必要的,可以考虑添加一个“市场”开关参数,它设置了<代码>参数集< /代码>。这就是谷歌搜索所导致的。有了“好”关键词和一些睡眠,我在下一个答案中找到了我发布的内容。