0

我的 PowerShell 脚本之一中有此代码:

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数组中的参数顺序发生了变化,我希望这个输出:

C:\command.exe anArg -other  2 1

但相反,我收到一个奇怪的消息:

C:\command.exe anArg -other  1 2

我错过了一些明显的东西吗?

4

1 回答 1

4

尝试像这样调用你的函数:

callCommandWithArguments "1" "2"

在 powershell 中,您将参数传递给函数,而不用()空格分隔。

在您的代码中,您正在传递一个类型的参数数组object[]

于 2012-10-25T13:43:42.870 回答