fault-tolerant splatting - by TobiasW

Status : 


Sign in
to vote
ID 778931 Comments
Status Active Workarounds
Type Suggestion Repros 0
Opened 2/11/2013 2:09:42 AM
Access Restriction Public


Splatting always forwards *all* parameters, so if a function uses additional parameters, they have to first be excluded them using $PSBoundParameters.Remove(). 

I suggest to add fault-tolerant splatting where splatting was smart enough to only forward those parameters that have matching parameters.
This would shield users from the complexity of removing keys from $PSBoundParameters and produce much simpler code.

It also would make $PSBoundParameters reusable, so it could be used against multiple cmdlets where each cmdlet only receives the parameters it supports.
Sign in to post a comment.
Posted by Kirk Munro on 2/11/2013 at 7:21 AM
I like this idea, however I don't like it being implicit because it makes the code harder to follow. With the example provided, the reader needs to know which parameters apply to the cmdlets where they are being splatted to understand how the function works.

I'm going to vote this up, however I only vote it up if the implementation supports specifying which parameters are splatted. Using the example provided, I would like to see that support implemented like this:

function test ($ComputerName, $Credential, $Property)
Get-WMIObject -Class Win32_BIOS @PSBoundParameters('ComputerName','Credential') | Select-Object @PSBoundParameters('Property')

This format allows for a script author to define which members in a splatted collection are passed through to the cmdlet being invoked. If no parameters are provided, the entire collection is splatted.

Further, there is one other splatting need that is not addressed here: the ability to splat a collection that is stored in a property of an object. That need is logged in a separate enhancement request, here: