0

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

Tom
  • 357
  • 1
  • 5
  • 18

1 Answers1

0

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'
Tom
  • 357
  • 1
  • 5
  • 18
  • Be a non-traditional magician and show a full example. Also, are you sure that parameter sets cannot be selected by distinct types? Might be worth asking a new question. – mklement0 Nov 18 '18 at 00:21
  • You're right, mklement0, my 1st finding was wrong because my test-code did not cover all test cases. I've added the current behavior. – Tom Nov 19 '18 at 23:29
  • PowerShell **sometimes** is able to detect the right ParameterSet depending on the passed arguments. But at least at our work, it usually does not work as expected and we're spending a lot of time to find workarounds. – Tom Nov 19 '18 at 23:32