powershelldynamicparameters

PowerShell: DynamicParam: get list of passed parameters?


Good afternoon

Unfortunately, PowerShell is not able to detect the ParameterSet by the Parameter Types, for example: If the 2nd Parameter is passed as a Int, then select ParameterSet1, otherwise use ParameterSet2.

Therefore I would like to manually detect the passed Parameter-Combinations.
Is it possible to get the list of passed parameters in DynamicParam, something like this?:

Function Log {
    [CmdletBinding()]
    Param ()
    DynamicParam {
        # Is is possible to access the passed Parameters?,
        # something like that:
        If (Args[0].ParameterName -eq 'Message') { … }

        # Or like this:
        If (Args[0].Value -eq '…') { … }
    }
    …
}

Thanks a lot for any help and light!
Thomas


Solution

  • This first finding was wrong!: "I've found the magic, by using $PSBoundParameters we can access the passed parameters."

    This is the correct but very disappointing answer:
    It's very annoying and unbelievable, but it looks like PowerShell does not pass any information about the dynamically passed arguments.

    The following example used the New-DynamicParameter function as defined here: Can I make a parameter set depend on the value of another parameter?

    Function Test-DynamicParam {
        [CmdletBinding()]
        Param (
            [string]$FixArg
        )
        DynamicParam {
            # The content of $PSBoundParameters is just 
            # able to show the Params declared in Param():
            # Key     Value
            # ---     -----
            # FixArg  Hello
    
            # Add the DynamicParameter str1:
            New-DynamicParameter -Name 'DynArg' -Type 'string' -HelpMessage 'DynArg help'
    
            # Here, the content of $PSBoundParameters has not been adjusted:
            # Key     Value
            # ---     -----
            # FixArg  Hello
        }
        Begin {
            # Finally - but too late to dynamically react! -
            # $PSBoundParameters knows all Parameters (as expected):
            # Key     Value
            # ---     -----
            # FixArg  Hello
            # DynArg  World
        }
        Process {
            …
        }
    }
    
    # Pass a fixed and dynamic parameter
    Test-DynamicParam -FixArg 'Hello' -DynArg 'World'