Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 函数中从写主机写入的变量的奇怪顺序_Powershell - Fatal编程技术网

Powershell 函数中从写主机写入的变量的奇怪顺序

Powershell 函数中从写主机写入的变量的奇怪顺序,powershell,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 - comma

在我下面的代码中,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:  

为什么像我预期的那样从main works编写主机,但在函数中它同时输出所有变量?如何更正此行为?

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


在IP地址变量中使用$args以外的值。

实际上,函数调用是错误的。它应该是
运行“ping”208.67.222.222“
。PowerShell中的函数调用不使用括号和逗号。