2012-10-25 3 views
0

J'ai ce code dans l'un de mes scripts PowerShell:Fonction paramètres ordre dans 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"); 

Comme les arguments commande est alors modifié dans le tableau $arguments, je me attends à cette sortie:

C:\command.exe anArg -other 2 1 

Mais à la place, je reçois un étrange:

C:\command.exe anArg -other 1 2 

Est-ce que je manque quelque chose d'évident?

Répondre

4

essayer d'appeler votre fonction comme ceci:

callCommandWithArguments "1" "2" 

En Powershell vous passez des arguments à fonctionner sans () et juste séparés par un espace.

Dans votre code que vous passsing un tableau unique argument de type object[]

+0

Grand merci! Je comprends maintenant ... –

+0

@YannickBlondeau Content d'aider! –

Questions connexes