function - PowerShell 中 $args 的混淆评估

标签 function powershell arguments

根据定义,$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/

相关文章:

c - 当我尝试编译 C 代码时,为什么会出现这些错误?

c - 如何在输入数组中打印出每个字符的地址和内容

c - 在此函数中将 system ("pause") 放在哪里?

powershell - 使用awk之类的powershell来获取控制台用户

WPF 将 ViewModel 属性传递给 Validation

javascript - 有限循环导致浏览器崩溃

function - golang 调用中的参数太多

python函数,传递多个参数但不使用它们?

pdf - PowerPoint 2007是否可以将幻灯片导出为包含动画的PDF?

azure - 使用 powershell 检查 Azure 存储帐户文件共享已用容量的快速方法