在函数内从Write-Host写入的变量的奇怪顺序

时间:2013-04-13 16:38:36

标签: powershell

使用我的以下代码,Write-Host似乎以一种奇怪的方式输出变量(至少来自C#)。

代码在这里

function Run(
[string] $command,
[string] $args
)
{
    Write-Host 'from function - command is:' $command '.args is: ' $args
}

$cmd = "ping"
$args = "208.67.222.222"
Write-Host 'from main - command is:' $cmd '.args is: ' $args
Run("ping","208.67.222.222")

输出在这里

from main - command is: ping .args is:  208.67.222.222
from function - command is: ping 208.67.222.222 .args is:  

为什么Write-Host来自主要工作,但是在函数内它同时输出所有变量?我该如何纠正这种行为?

1 个答案:

答案 0 :(得分:1)

函数中的$ args是一个自动变量。它是一个包含传递给函数的所有参数的数组。

使用$ args以外的内容作为您的IP地址变量。