Suppose I set 3 parameters:a,b,c
`b` and `c` are dynamic parameters, shown and hidden according to the value of `a`
Suppose a = 1 b = 1;2 c=1;2;5;6;7
a = 2 c=6;7
PS >test {tab}
a
PS >test -a 1 -{tab}
b c
PS >test -a 2 -{tab}
c
PS >test -a 2 -c {tab}
6 7
Hi There,
Go through the script below on Dynamic Parameters from the GitHub and start building up on your own, and please let us know if you stuck any where…
https://github.com/RamblingCookieMonster/PowerShell/blob/master/New-DynamicParam.ps1
Also there is another feature called Argument Completer, that could also be helpful to you, please refer to my blog post below…
https://kpatnayakuni.com/2020/01/22/powershell-argumentcompleter-vs-validateset-and-register-argumentcompleter/
Thank you.
The script does not support multiple parametersetname
I still don’t understand how to display dynamic parameters
Hi,
This is a sample script to show you how it works and better understanding of the concept, it may not be the exact fit for your requirement, you need to modify it according to your requirements, and if you stuck anywhere then we are happy to help you.
I request you to please go through some online help on advanced functions and parameters for better understanding of the concept.
Thank you.
Using DynamicParam only I don’t know what to do
[pre]
function test {
[CmdletBinding()]
param()
DynamicParam
{
function ParamDictionary{
begin{$ParamDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary}
process{
$AttributeCollection=New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$Attributes=New-Object System.Management.Automation.ParameterAttribute
$AttributeCollection.add($Attributes)
if ($.Argument){
$AttributeCollection.Add((New-Object System.Management.Automation.ArgumentCompleterAttribute($.Argument)))
}
$RuntimeDefinedParameter=New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($.name,$.type,$AttributeCollection)
$ParamDictionary.Add($_.name,$RuntimeDefinedParameter)
}
end{$ParamDictionary}
}
$Argument=
{param ( $commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters
)
$possibleValues = @{
b = @(‘graphs’,‘boot’,‘reboot’,‘shutdown’,‘Destroy’,‘info’,‘status’,‘list-os’,‘list’,“reinstall”,“create”,“resize”,“reset-password”)
a = @(‘hypervisors’,‘boot’,‘graphs’,‘info’,‘reboot’,‘shutdown’,‘status’)
}
if ($fakeBoundParameters.ContainsKey(‘b’))
{
$possibleValues[‘b’] | Where-Object {
$_ -like “$wordToComplete*”
}
}
elseif ($fakeBoundParameters.ContainsKey(‘a’))
{
$possibleValues[‘a’] | Where-Object {
$_ -like “$wordToComplete*”
}
}}
‘a’…‘e’|%{
$object=@{name=$;type=‘string’}
if($ -eq ‘c’){$object+=@{Argument=$Argument}}
$object}|ParamDictionary
}
}[/pre]
I can’t find a valid method
Test |
-a |
-b |
-c |
a |
|
|
|
|
|
B |
-d |
|
|
|
|
C |
-e |
|
|
|
|
D |
[pre]DynamicParam
{
$notid=@('list','list-os','create','hypervisors')
if ($notid -notcontains $Name){
$Attributes = New-Object System.Management.Automation.ParameterAttribute
$Attributes.Mandatory = $false
$Attributes.ParameterSetName = '__AllParameterSets'
$Attributes.ValueFromPipeline = $false
$Attributes.ValueFromPipelineByPropertyName = $false
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$AttributeCollection.Add($Attributes)
#$AttributeCollection.Add((New-Object System.Management.Automation.ValidateSetAttribute(‘’)))
$ParamDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
$dname=“id”
$ParamDictionary.Add($dname,(New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter($dname,[int32],$AttributeCollection)))
return $ParamDictionary
}
}[/pre]
I see you’ve cross-posted here from another site that I responded to you on.
DynmaicParams is an advanced topic (not really something folks new to PowerShell, but coming from another language (and I am assuming here) should do as a first thing), and as noted, you really need to spend some time ramping up on it to fully understand them and as such, limit the confusion you’d encounter, have encountered thus far.
Otherwise, you are asking folks to write the code for you and your pseudo-code is really not complete enough to dig at what you are after, thus forcing folks to give you generic and pointer responses for you to read up on.
Again, as I said in my other response, that there are plenty of examples of this DynamicParam thing all over the web. For Example:
Dynamic Parameters in PowerShell
Function Get-Order {
[CmdletBinding()]
Param(
[Parameter(
Mandatory=$true,
Position=1,
HelpMessage="How many cups would you like to purchase?"
)]
[int]$cups,
[Parameter(
Mandatory=$false,
Position=2,
HelpMessage="What would you like to purchase?"
)]
[ValidateSet("Lemonade","Water","Tea","Coffee")]
[string]$product="Lemonade"
)
Process {
$order = @()
for ($cup = 1; $cup -le $cups; $cup++) {
$order += "$($cup): A cup of $($product)"
}
$order
}
}
@postanote
Your example is meaningless, so I have seen the webpage long ago。
I do n’t need you to write me an answer, you just need to provide the method,