根据定义,$args 变量应该包含传递给脚本函数的所有参数。但是,如果我在我的函数内构造一个管道,$args 变量的计算结果为 null。谁知道为什么?
看这个例子:
function test { 1..3 | % { echo "args inside pipeline: $args" } ; echo "args outside pipeline: $args" }
这是传递参数“hello”时的输出:
PS> test hello
args inside pipeline:
args inside pipeline:
args inside pipeline:
args outside pipeline: hello
这有什么具体原因吗?我知道如何解决这个问题,但我想知道是否有人可以解释这个问题的原因。
最佳答案
管道使用 $input。试试这个:
function test { 1..3 | % { echo "args inside pipeline: $input" } ; echo "args outside pipeline: $args" }
关于function - PowerShell 中 $args 的混淆评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/449981/