Powershell:将参数和管道输入转发到alias函数

Powershell:将参数和管道输入转发到alias函数,powershell,Powershell,如何将所有管道输入和参数转发到alias函数内的命令 例如,如果我想要别名tail function tail { coreutils tail @args } 适用于tail-n5 test.txt 但不适用于cat test.txt|tail-n 5 即使在最简单的情况下,cat test.txt | coreutils tail-n 5也能工作,请使用以下方法: 函数尾部{ 如果($MyInvocation.ExpectingInput){#存在管道输入。 #$Input将收集的管道

如何将所有管道输入和参数转发到alias函数内的命令

例如,如果我想要别名tail

function tail {
  coreutils tail @args
}
适用于
tail-n5 test.txt

但不适用于
cat test.txt|tail-n 5


即使在最简单的情况下,
cat test.txt | coreutils tail-n 5
也能工作

,请使用以下方法:

函数尾部{
如果($MyInvocation.ExpectingInput){#存在管道输入。
#$Input将收集的管道输入传递给。
$Input| coreutils tail@args
}否则{
coreutils tail@args
}
}
这种方法的缺点是,所有管道输入首先在内存中收集,然后再转发到目标程序


流媒体解决方案——其中输入对象(行)在可用时通过——需要付出更多努力:

函数尾部{
[CmdletBinding(positionBinding=$false)]
param(
[参数(ValueFromPipeline)]
$InputObject
,
[参数(ValueFromRemainingArguments)]
[字符串[]]$PassThruArgs
)
开始
{
#建立一个可分级的管道。
$scriptCmd={coreutils tail$PassThruArgs}
$steppablePipeline=$scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
$steppablePipeline.Begin($PSCmdlet)
}
过程
{
#传递当前管道输入。
$steppablePipeline.Process($\ux)
}
结束
{
$steppablePipeline.End()
}
}
上面是一个所谓的代理函数,将在中详细解释