Building on this technique to use Cmdlets as "delegates" I am left with this question:
Is there a way to pass a commandlet with prescribed named or positional parameters to another commandlet that uses the powershell pipeline to bind the remaining parameters to the passed commandlet?
Here is the code snippet I'd like to be able to run:
Function Get-Pow{
[CmdletBinding()]
Param([Parameter(ValueFromPipeline=$true)]$base,$exp)
PROCESS{[math]::Pow($base,$exp)}
}
Function Get-Result{
[CmdletBinding()]
Param([Parameter(ValueFromPipeline=$true)]$x,$Cmdlet)
$x | . $Cmdlet
}
10 | Get-Result -Cmdlet 'Get-Pow -exp 2'
10 | Get-Result -Cmdlet 'Get-Pow -exp 3'
10 | Get-Result -Cmdlet Get-Pow -exp 2
10 | Get-Result -Cmdlet Get-Pow -exp 3
The first two calls to Get-Result
result in CommandNotFoundException
because Get-Pow -exp 2
"is not the name of a cmdlet."
The last two calls to Get-Result
result in NamedParameterNotFound,Get-Result
since that syntax is actually attempting to pass parameter -exp
to Get-Result
which it does not have.
Is there some other way to set this up so it works?