none
DynamicParams with Mandatory parameters

    Question

  • Hi ,

    got into a problem with DynamicParams,

      an example code

      function test{
          [Cmdletbinding()]
          Param(
              [Parameter(Mandatory)]
              $r
          )
      
      DynamicParam {
          if( $r -eq 'Yes' ){
      
              $ParameterDictionary        = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
              $ParamAttribute             = New-Object -TypeName System.Management.Automation.ParameterAttribute
              $ParamAttribute.Mandatory   = $True
              $AttributeCollection        = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]
              $AttributeCollection.Add( $ParamAttribute )        
              $Param                      = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter('t', [String], $AttributeCollection)    
              $ParameterDictionary.Add('t',$Param) | out-null
          return $ParameterDictionary
      
          }
      
      }
      Process{
          $PSBoundParameters
      }
      
      }
      

      when I execute by mentioning -r and with the expected 'yes' value, Parameter 'r' is having the value in it in DynamicParam{} and if loop passes.

      test -r 'yes

      when I execute without mentioning -r and giving the expected value 'yes' when prompted as its mandatory, Parameter 'r' is not having the value in it in DynamicParam{} and if loop fails.

      test

      I can see key 'r' in the $PSBoundParameters, but the value is empty,

      any idea ?


    Regards kvprasoon

    Monday, November 13, 2017 1:01 PM

All replies

  • Hi,

    Based on the tests in my lab, the same behavior is re-produced. I suppose this behavior is by design. In this case, to avoid issues when using dynamic parameters, I recommend set the parameter which does not participate in judgment to Mandatory = $true and set the parameter which participates in judgment to Mandatory = $false.

    For more information, please refer to the following article:
    Dynamic Parameters in PowerShell
    http://www.powershellmagazine.com/2014/05/29/dynamic-parameters-in-powershell/

    Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, November 14, 2017 9:06 AM
  • This problem basically doesn't have any relation to the Mandatory attribute,

    when its set as Mandatory, at least it will prompt and I can take decision and if not mandatory and not specified while invoking, for sure the if loop fails.

    Why the Parameter ('r' here) under param block is not bound with its provided value in DynamicParam block if not given while invoking but gave while prompted,


    Regards kvprasoon

    Tuesday, November 14, 2017 3:20 PM
  • Post this in UserVoice.  It's a deficiency in how the parameters are handled.


    \_(ツ)_/

    Tuesday, November 14, 2017 3:35 PM
    Moderator
  • thanks @jrv,

    if you wanna vote


    Regards kvprasoon


    Tuesday, November 14, 2017 4:06 PM
  • The workaround would be to make "$r" a dynamic parameter also to to use parameter sets to alter behavior.


    \_(ツ)_/

    Tuesday, November 14, 2017 4:16 PM
    Moderator
  • Unfortunately, this behavior holds good within DynamicParams.

    If 'r' is a dynamicParam and if I want to use the $r within the block, it will not be bound to with the value but it will be bound to the $Myinvocation.BoundParameters with an empty 'value' property


    Regards kvprasoon


    Tuesday, November 14, 2017 4:43 PM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, November 15, 2017 7:21 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, November 17, 2017 5:53 AM