Function PowerShell中的函数参数顺序
我的一个PowerShell脚本中包含以下代码:Function PowerShell中的函数参数顺序,function,powershell,parameters,Function,Powershell,Parameters,我的一个PowerShell脚本中包含以下代码: function callCommandWithArguments([String] $arg1, [String] $arg2) { [string]$pathToCommand = "C:\command.exe"; [Array]$arguments = "anArg", "-other", "$arg2", "$arg1"; # the real code is # & $pathToCommand $argu
function callCommandWithArguments([String] $arg1, [String] $arg2)
{
[string]$pathToCommand = "C:\command.exe";
[Array]$arguments = "anArg", "-other", "$arg2", "$arg1";
# the real code is
# & $pathToCommand $arguments;
# but was not working, so I change it to debug
Write-Host $pathToCommand $arguments;
}
callCommandWithArguments("1", "2");
由于$arguments
数组中的arguments顺序已更改,因此我希望得到以下输出:
C:\command.exe anArg -other 2 1
但我却收到了一个奇怪的消息:
C:\command.exe anArg -other 1 2
我是否遗漏了一些明显的内容?尝试这样调用函数:
callCommandWithArguments "1" "2"
在powershell中,您将参数传递给函数,而不使用()
,仅用空格分隔
在您的代码中,您正在传递一个类型为
object[]
的单参数数组,请尝试如下调用您的函数:
callCommandWithArguments "1" "2"
在powershell中,您将参数传递给函数,而不使用()
,仅用空格分隔
在您的代码中,您正在传递一个类型为
object[]
@YannickBlondeau的单参数数组。很高兴为您提供帮助@YannickBlondeau很乐意帮忙!