Parameters with default value not in PsBoundParameters?

Posted by stej on Stack Overflow See other posts from Stack Overflow or by stej
Published on 2010-05-11T08:07:41Z Indexed on 2010/05/11 8:14 UTC
Read the original article Hit count: 387

General code

Consider this code:

PS> function Test { param($p='default value') $PsBoundParameters }
PS> Test 'some value'
Key                                                               Value
---                                                               -----
p                                                                 some value
PS> Test
# nothing

I would expect that $PsBoundParameters would contain record for $p variable on both cases. Is that correct behaviour?

Question

I'd like to use splatting for a lot of functions that would work like this:

function SomeFuncWithManyRequiredParams {
  param(
    [Parameter(Mandatory=$true)][string]$p1,
    [Parameter(Mandatory=$true)][string]$p2,
    [Parameter(Mandatory=$true)][string]$p3,
  ...other parameters
  )
  ...
}
function SimplifiedFuncWithDefaultValues {
  param(
    [Parameter(Mandatory=$false)][string]$p1='default for p1',
    [Parameter(Mandatory=$false)][string]$p2='default for p2',
    [Parameter(Mandatory=$false)][string]$p3='default for p3',
  ...other parameters
  )
  SomeFuncWithManyRequiredParams @PsBoundParameters
}

I have more functions like this and I don't want to call SomeFuncWithManyRequiredParams with all the params enumerated:

  SomeFuncWithManyRequiredParams -p1 $p1 -p2 $p2 -p3 $p3 ...

Is it possible?

© Stack Overflow or respective owner

Related posts about powershell

Related posts about powershell-v2.0