Product: PowerShell Universal
Version: 3.7.8
Hello,
unfortunately I could not find anything about dynamic parameters in the documentation or here in the forum.
Scenario:
I want to provide a script as API that has multiple parameters. In addition, there is an area in the script called “DynamicParam” which I use to provide variable choices for this function and any other input for this parameter would result in an error.
Now I have noticed that when I configure such a function as an API and want to populate the parameter, Powershell Universal simply ignores the dynamic parameter or doesn’t know it exists.
How can I address dynamic parameters correctly? Basically I just want to pass a string via the API to a dynamic parameter within my function.
Is there anything to look up here? Or does this possibility not exist yet?
If it doesn’t exist yet I would file a feature request for this.
Function MyFunction {
[CmdletBinding()]
param(
[Parameter(Mandatory)][ValidateNotNullOrEmpty()][String]$StringParam,
[Parameter(Mandatory=$false)][ValidateNotNullOrEmpty()][Switch]$SwitchParam
)
DynamicParam{
# Create a dynamic param (I'm using a 3rd-party function)
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$Users = Get-ADUser -Filter * | Where-Object {$_.SamAccountName -match '^\d{7}$'}
New-DynamicParam -Name SamAccountName -ValidateSet $Users.SamAccountName -DPDictionary $Dictionary -ParameterSetName ID
New-DynamicParam -Name GivenName -ValidateSet $Users.GivenName -DPDictionary $Dictionary -ParameterSetName GivenName
New-DynamicParam -Name SurName -ValidateSet $Users.SurName -DPDictionary $Dictionary -ParameterSetName SurName
$Dictionary
}
Begin{
# prepare any stuff
}
Process{
# process any stuff
}
End{
# end any stuff
}
}