有没有办法将一些参数传递给另一个Cmdlet的Cmdlet,将其余的参数传递给它? [英] Is there a way pass a Cmdlet with some parameters to another Cmdlet that pipes the remaining parameters to it?

查看:148
本文介绍了有没有办法将一些参数传递给另一个Cmdlet的Cmdlet,将其余的参数传递给它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于使用Cmdlet作为代理的技巧我留下了这个问题:

Building on this technique to use Cmdlets as "delegates" I am left with this question:

有没有办法将具有规定的命名或位置参数的命令行传递给另一个使用powershell管道将其余参数绑定到传递的命令行的命令行?

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

前两个调用 Get-Result result in CommandNotFoundException 因为 Get-Pow -exp 2 不是一个cmdlet的名称。

The first two calls to Get-Result result in CommandNotFoundException because Get-Pow -exp 2 "is not the name of a cmdlet."

最后两次调用 Get-Result 导致 NamedParameterNotFound,Get-Result ,因为该语法实际上试图将参数 -exp 传递给 Get-Result 它没有。

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?

推荐答案

我不知道这是最好的方法,但至少它类似于powerhell成语:

I'm not sure this is the best method, but at least it resembles powershell idioms throughout:

Function Get-Amount{
    [CmdletBinding()] 
    Param(
    [Parameter(ValueFromPipeline=$true)]$t,
    [Parameter(position=1)]$r,
    [Parameter(position=2)]$P)
PROCESS{$P*[math]::Pow(1+$r,$t)}
}
Function Get-Result{
    [CmdletBinding()]
    Param(
    [Parameter(ValueFromPipeline=$true)]$x,
    [Parameter(Position=1)]$Cmdlet,        #positional arguments here makes calling more readable
    [Alias('args')]                        #careful, $args is a special variable
    [Parameter(Position=2)]$Arguments=@()) #the default empty array is required for delegate style 1
PROCESS{
    #invoke style 1                        #works with delegate styles 1,2,3
    iex "$x | $Cmdlet @Arguments"          

    #invoke style 2                        #works with delegate styles 2,3
    $x | . $Cmdlet @Arguments              
}}

5,20 | Get-Result 'Get-Amount -r 0.05 -P 100' #delegate style 1
5,20 | Get-Result Get-Amount 0.05,100         #delegate style 2
5,20 | Get-Result Get-Amount @{r=0.05;P=100}  #delegate style 3

哪些导致:

127.62815625
CommandNotFoundException
265.329770514442
CommandNotFoundException
127.62815625
127.62815625
265.329770514442
265.329770514442
127.62815625
127.62815625
265.329770514442
265.329770514442

这篇关于有没有办法将一些参数传递给另一个Cmdlet的Cmdlet,将其余的参数传递给它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆