Using default values assigned to parameters within a dynamic parameter

Welcome Forums General PowerShell Q&A Using default values assigned to parameters within a dynamic parameter

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
2 weeks, 2 days ago.

  • Author
    Posts
  • #126814

    Participant
    Points: 110
    Helping Hand
    Rank: Participant

    I am working on a function with a normal string parameter, with a default values assigned, and 1 dynamic parameter. My goal is for the dynamic parameter to use the string value in the first parameter. The code works if I define the -Path parameter on the commandline, but it doesn't seem to work with the default value. I have checked the $PSBoundParameters variable and it is empty when I call it in the dynamic parameter block. Does anyone have any advice for this? I want to provide a default location for my function, with the flexibility for the user to define their own path if desired. Thanks for any advice!

    function Test-Function
    {
        [CmdletBinding()]
        Param
        (
            # Param1 help description
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            [string]$Path = 'C:\Temp\SubFolder1'
        )
        DynamicParam {
                # Set the dynamic parameters' name
                $ParameterName = 'FileName'
                
                # Create the dictionary 
                $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
                # Create the collection of attributes
                $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
                
                # Create and set the parameters' attributes
                $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
                $ParameterAttribute.Mandatory = $true
                $ParameterAttribute.Position = 1
    
                # Add the attributes to the attributes collection
                $AttributeCollection.Add($ParameterAttribute)
    
                # Generate and set the ValidateSet 
                $arrSet = Get-ChildItem -Path $Path -File | select -ExpandProperty Name
                $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
    
                # Add the ValidateSet to the attributes collection
                $AttributeCollection.Add($ValidateSetAttribute)
    
                # Create and return the dynamic parameter
                $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
                $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
                return $RuntimeParameterDictionary
        }
    
        Begin 
        {
          # Bind the parameter to a friendly variable
          $FileName = $PsBoundParameters[$ParameterName]
        }
        Process
        {
          $FileName
        }
        End
        {
        }
    }
  • #126884

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    From my experience, Parameter values wont be available in DynamicParam block. But you can check whether the parameter is bound or not with $PSBoundParameters.
    Make the $Path parameter to validate for NotNull and EmptyString and check if the Parameter $File is bound or not for asking FileName Dynamically.

  • #127208

    Participant
    Points: 110
    Helping Hand
    Rank: Participant

    kvprasoon – Thanks for the response, as mentioned above I checked $PSBoundParameters before posting. I added the [ValidateNotNullOrEmpty()] decoration and checked $PSBoundParameters again and no go. This is something I have been playing with off and on for some time now and haven't been able to resolve which is why I finally broke down and posted here. I think the $PSDefaultParameterValues is going the be the solution I will use to achieve my goal. Unless anyone else has a better idea. Thanks again for your time!

  • #127299

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    checked $PSBoundParameters again and no go

    As you are aware, [ValidateNotNullOrEmpty()] is added just to make sure null or empty string is not passed to $Path parameter. with this $PSBoundParameters.Path will not get any value, but you can check whether $PSBoundParameters.ContainsKey('Path') is true or false. If it is true then you don't need to ask for $FilePath else, ask for it.

You must be logged in to reply to this topic.