Powershell 函数中从写主机写入的变量的奇怪顺序
在我下面的代码中,Write-Host似乎以一种奇怪的方式输出变量(至少对于来自C#的我来说) 代码在这里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
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中的函数调用不使用括号和逗号。