Get ValueFromRemainingArguments as an hashtable

前端 未结 4 1873
时光说笑
时光说笑 2020-12-10 07:23

Using [parameter(ValueFromRemainingArguments=$true)] one can get all the remaining arguments passed to the function into a variable as a list.

How can I

4条回答
  •  孤城傲影
    2020-12-10 08:18

    This is tricky because using [Parameter(ValueFromRemainingArguments=$true)] means this is an advanced function, and $args cannot be used in advanced functions.

    But if you want a hashtable of all the specified parameters and their values, you could simply use $PSBoundParameters, like so :

    function foo {
        [cmdletbinding()]
    
        param( 
            [Parameter(Position=0)]       
            $Name,
    
            [Parameter(Position=1,ValueFromRemainingArguments=$true)]
            $LastName
        )
    
        "PSBoundParameters : " 
        $PSBoundParameters
    
    }
    foo -Name Mike Jordan Jones
    

    This results in the following :

    PSBoundParameters : 
    
    Key                                               Value                                           
    ---                                               -----                                           
    Name                                              Mike                                            
    LastName                                          {Jordan, Jones}         
    

    Is this what you are trying to achieve ?

提交回复
热议问题