Dynamic parameter disappears, but only if typed second.
I'm writing a function with a DynamicParam code block defining a parameter attribute, but there is very definitely strange behavior going on. In the following function I have three separate parameter attribute definitions for the $Subnet mask parameter. The first one is what I want in the end. The second one is a short list just to see if the length of the list changed things (spoiler alert; it didn't). The third one was a test to see if the data within it changed things (spoiler alert; it DID).All right. The bottom line is that this function works with the any of these parameter attribute definitions so long as you type the Application parameter first when using the function. If you type the SubnetMask parameter first, then the dynamic parameter called Application is no longer available. But here’s where it gets interesting. Change the list of values from IPs to anything else (I used fruit) and it stays visible and the function works great.
Is there something I need to change to make sure my Application parameter doesn’t disappear?
$TheseApps = @('this','that','other') function Get-This { [CmdletBinding()] param( [ValidateSet('255.0.0.0','255.128.0.0','255.192.0.0','255.224.0.0','255.252.0.0','255.254.0.0','255.255.0.0','255.255.128.0','255.255.192.0','255.255.224.0','255.255.240.0','255.255.248.0','255.255.252.0','255.255.254.0','255.255.255.0','255.255.255.128','255.255.255.192','255.255.255.224','255.255.255.240','255.255.255.248','255.255.255.252','255.255.255.254','255.255.255.255')] #[ValidateSet('255.0.0.0','255.128.0.0')] #[ValidateSet('banana','cherry','apple','kiwi','peach')] [string] $SubnetMask ) DynamicParam { # Parameter Attribute $AppAttribute = New-Object System.Management.Automation.ParameterAttribute -Property @{ Mandatory = $true Position = 0 HelpMessage = 'Select from the list of applications' ValueFromPipeline = $false ValueFromPipelineByPropertyName = $false DontShow = $false } # Values Collection $attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute] $attributeCollection.Add( $AppAttribute ) $ValidateSet = New-Object System.Management.Automation.ValidateSetAttribute( $TheseApps ) $attributeCollection.Add( $ValidateSet ) # Runtime Defined Parameter $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter( "Application", [string], $attributeCollection ) # Runtime Defined Parameter Dictionary $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary $paramDictionary.Add( "Application", $dynParam1 ) return $paramDictionary } Begin{} Process { foreach( $Key in $PSBoundParameters.Keys ) { Write-Host "$Key is $( $PSBoundParameters.Item($Key) )" } } End{} }