Search

fault-tolerant splatting by TobiasW

Active

6
0
Sign in
to vote
Type: Suggestion
ID: 778931
Opened: 2/11/2013 2:09:42 AM
Access Restriction: Public
0
Workaround(s)
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.

Details (expand)
How often does this happen?
Always Happens
Have you seen this problem before in this product?
Yes, this happens in all previous versions
Reproduction Steps

# fault-tolerant splatting would enable forwarding
# parameters to multiple cmdlets, each picking the properties
# it supports.
# Today, the scripter has to take $PSBoundParameters and remove
# keys individually to produce tailored hash tables for each cmdlet

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

}
Expected Results
with fault-tolerant splatting, the above example would work and enable the user to forward different parameters to different cmdlets.
File Attachments
0 attachments
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:

https://connect.microsoft.com/PowerShell/feedback/details/778964/add-support-for-splatting-collections-that-are-stored-as-a-property-of-an-object
Sign in to post a workaround.