Thanks again to Rohn Edwards, who helped me with my first DynamicParam problem. Maybe he or someone can help me with what I hope is my last issue…
Having a hard time positioning an established Dynamic Parameter (Set1C in this example) in the right location. The code at the bottom, which starts with everything in the same ParameterSetName gets me this:
PS G:\> help ParamTest NAME ParamTest SYNTAX ParamTest [-A] (string) [-Set1] [-Set2] [-Set1B] (string) [-Set1C] (string) {Option1,Option2,Option3} [-Set2B] (string) [CommonParameters] ALIASES None REMARKS None
I’m trying to modify it so that the syntax shows like this:
PS G:\> help ParamTest NAME ParamTest SYNTAX ParamTest [-A] (string) [-Set1] [-Set1B] (string) [-Set1C] {Option1,Option2,Option3} (string) [CommonParameters] ParamTest [-A] (string) [-Set2] [-Set2B] (string) [CommonParameters] ALIASES None REMARKS None
Every way I try to do this ends up with the Dynamic Parameter (Set1C) just disappearing from the parameter list…
The other thing I notice is that, even though the ordering shows Set1C in the 5th position when you use help, when you actually tab through the parameters on the command line, in populates Set1C last, ignoring the position attribute. I assume it’s because the DynamicParam section is being evaluated after the Param section, but is there any way around that?
Here’s the code. Thanks to anyone who can offer assistance!
Function ParamTest { [CmdletBinding()] Param ( [Parameter(Mandatory=$true,Position=1,ParameterSetName="__AllParameterSets")] [string] $A, [Parameter(Mandatory=$true,Position=2,ParameterSetName="__AllParameterSets")] [switch] $Set1, [Parameter(Mandatory=$true,Position=3,ParameterSetName="__AllParameterSets")] [switch] $Set2, [Parameter(Mandatory=$true,Position=4,ParameterSetName="__AllParameterSets")] [string] $Set1B, [Parameter(Mandatory=$true,Position=6,ParameterSetName="__AllParameterSets")] [string] $Set2B ) DynamicParam { # Set the dynamic parameters' name $ParameterName = 'Set1C' # Create the dictionary $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary # Create the collection of attributes $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] # Create and set the parameters' attributes $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $true $ParameterAttribute.Position = 5 $ParameterAttribute.ValuefromPipeline = $true $ParameterAttribute.ValueFromPipelineByPropertyName = $true $ParameterAttribute.ParameterSetName = "__AllParameterSets" # Add the attributes to the attributes collection $AttributeCollection.Add($ParameterAttribute) # Generate and set the ValidateSet $arrSet = ("Option1,Option2,Option3") $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) # Add the ValidateSet to the attributes collection $AttributeCollection.Add($ValidateSetAttribute) # Create and return the dynamic parameter $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) return $RuntimeParameterDictionary } }