Reputation: 71
I know how to use ValidatetSet and ParameterSetName. I need to change dynamically the value of a ParameterSetName according to a previous value selected in ValidateSet. Is this possible?
it seems that Dynamic Parameters exist: https://social.technet.microsoft.com/wiki/contents/articles/15994.powershell-advanced-function-parameter-attributes.aspx#Dynamic_Parameters
My use case:
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
)
many conditions needed, e.g if 'Market' is selected from $OrderType
then $quoteOrderQty
is required and then should be part of the same ParameterSetName.
Hope I'm clear enough, please advise if you have a better approach. Thanks a lot
Yann
Upvotes: 0
Views: 356
Reputation: 71
Dynamic parameters are parameters of a cmdlet, function, or script that are available only under certain conditions. You can also create a parameter that appears only when another parameter is used in the function command or when another parameter has a certain value.
The DP1 parameter is available in the Get-Sample function only when the value of the Path parameter starts with HKLM:
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
}
}
}
Upvotes: 1