How to create dynamic parameters

Welcome Forums General PowerShell Q&A How to create dynamic parameters

  • This topic has 7 replies, 3 voices, and was last updated 3 weeks ago by
    OK
    Participant
    .
Viewing 7 reply threads
  • Author
    Posts
    • #201660
      OK
      Participant
      Topics: 5
      Replies: 17
      Points: 60
      Rank: Member

      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
      
      • This topic was modified 3 weeks, 2 days ago by OK.
      • This topic was modified 3 weeks, 2 days ago by OK.
    • #201672
      Moderator
      Topics: 3
      Replies: 200
      Points: 1,052
      Helping Hand
      Rank: Community Hero

      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.

    • #201882
      OK
      Participant
      Topics: 5
      Replies: 17
      Points: 60
      Rank: Member

      The script does not support multiple parametersetname

       

      I still don’t understand how to display dynamic parameters

    • #201888
      Moderator
      Topics: 3
      Replies: 200
      Points: 1,052
      Helping Hand
      Rank: Community Hero

      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.

    • #201987
      OK
      Participant
      Topics: 5
      Replies: 17
      Points: 60
      Rank: Member

      Using DynamicParam only I don’t know what to do

      Test -a -b a
      B -C
      C
      D -B
      
      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
      }
      }
      • This reply was modified 3 weeks ago by OK.
      • This reply was modified 3 weeks ago by OK.
      • This reply was modified 3 weeks ago by OK.
      • This reply was modified 3 weeks ago by OK.
    • #202037
      OK
      Participant
      Topics: 5
      Replies: 17
      Points: 60
      Rank: Member

      I can’t find a valid method

      Test -a -b -c a
      B -d
      C -e
      D
      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
      }
      }

       

    • #202055
      Participant
      Topics: 2
      Replies: 1013
      Points: 2,077
      Helping Hand
      Rank: Community Hero

      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

      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
         }
      }
      
      • This reply was modified 3 weeks ago by postanote.
    • #202082
      OK
      Participant
      Topics: 5
      Replies: 17
      Points: 60
      Rank: Member

      @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,

Viewing 7 reply threads
  • You must be logged in to reply to this topic.