令人困惑的评估PowerShell中的$ args

dri*_*iAn 3 powershell arguments function

根据定义,$ args变量应包含传递给脚本函数的所有参数.但是,如果我在我的函数中构造一个管道,$ args变量的计算结果为null.谁知道为什么?

看这个例子:

function test { 1..3 | % { echo "args inside pipeline: $args" } ; echo "args outside pipeline: $args" }
Run Code Online (Sandbox Code Playgroud)

传递参数"hello"时,这是输出:

PS> test hello
args inside pipeline:
args inside pipeline:
args inside pipeline:
args outside pipeline: hello
Run Code Online (Sandbox Code Playgroud)

这有什么特别的原因吗?我知道如何解决这个问题,但是我想知道那里的anonye是否可以解释这个原因.

EBG*_*een 5

管道使用$ input.试试这个:

function test { 1..3 | % { echo "args inside pipeline: $input" } ; echo "args outside pipeline: $args" }
Run Code Online (Sandbox Code Playgroud)