Need a clean way to splat a hashtable against the various commands that take an ArgumentList
Votes from Connect: 11
Original Date Submitted: 8/16/2013 7:15:31 AM
Description:
Contact Information
Handle: Keith Hill MVP
Site Name: PowerShell
Feedback ID: 797536
Frequency: Always Happens
Regression: Yes, this happens in previous released versions
Problem Description:
There are several commands that have an ArgumentList parameter that take arguments bound for a PowerShell command. However, this parameter doesn't support splatting in any sort of easy and/or obvious way.
Product Studio item created by Connect Synchronizer due to creation of feedback ID 797536 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=797536).
Repro Steps:
create a file $home\icmargs.ps1 with these contents:
Param(
[Parameter(Mandatory, Position=0)]
[string]$Computername=$env:computername,
[string]$log,
[int]$newest=5
)
"ComputerName is $Computername, log is $log, newest is $newest"
Now try to invoke like so:
13# icm localhost .\icmargs.ps1 -ArgumentList foo,bar,10
A positional parameter cannot be found that accepts argument 'bar'.
Doh! Positional parameters not going to cut it. That's OK I'd rather use a hashtable and then splat it:
14# $ht = @{ComputerName='foo';Log='mylog';newest=10}
15# icm localhost .\icmargs.ps1 -ArgumentList @ht
Invoke-Command : Missing an argument for parameter 'ArgumentList'. Specify a parameter of type 'System.Object[]' and try again.
Crud! Still no joy.
Expected Results:
I expect that all the commands that take an ArgumentList and then target a PowerShell command should support splatting with a hashtable. Some of those commands are:
Invoke-Command, Start-Job, Trace-Command, New-Module, Import-Module, Import/Export-PSSession, Get-Command
Now you could add additional parameter sets for each command to support a switch such as -SplatArguments. Another option is to first recognize splatting directly e.g.
icm localhost .\icmargs.ps1 -ArgumentList @arglist
where $arglist is defined like so:
$arglist = @{ComputerName='foo';log='file.log';newest=10}
And second, allow for literal hashtable splatting which would like require a syntax change e.g.
icm localhost .\icmargs.ps1 -ArgumentList @@{ComputerName='foo';log='file.log';newest=10}.
Internal BugId: 5349
