无法调用带有参数的命令

无法调用带有参数的命令

我已经花了几个小时才解决这个问题,因为我到处寻找解决方案。我可能还不了解这个问题,所以对我来说没有什么方法可行。

我使用 powershell 创建了一个必须在远程计算机上执行的命令。为了演示,我先在本地进行了测试,但没有成功。这是代码和错误:

#Defining Date
$DateYesterday =  (Get-Date).adddays(-1)
$dateyesterdayformat = Get-Date $dateyesterday -UFormat %Y/%m/%d
$DateToday = (Get-Date -UFormat %Y/%m/%d)

#creating command
$command1 = &("C:\Program Files (x86)\CLITOOL\tool.exe display statistics " +  $dateyesterdayformat + " " + $DateToday + " *")

最终的字符串看起来如下并且是正确的:

C:\Program Files (x86)\CLITOOL\tool.exe display statistics 2016/08/15 2016/08/16 *

尝试在本地服务器上执行此命令时:

&$command1

我收到此错误:

The term 'C:\Program Files (x86)\CLITOOL\tool.exe display statistics 2016/08/15 2016/08/16 *' is not recognized as the name of a cmdlet,     function, script fil
e, or operable program. Check the spelling of the name, or if a path was includ
ed, verify that the path is correct and try again.
At line:1 char:20
+ $command1 = & <<<< ("C:\Program Files (x86)\CLITOOL\tool.exe display statistics " +  $dateyesterdayformat + " " + $DateToday + " *")
+ CategoryInfo          : ObjectNotFound: (C:\Program File...15     2016/08/16
    *:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我确实看到了问题,但我不知道如何解决它。我如何创建一个命令,在其中我可以传递这些日期和字符串作为参数并让它们被调用?

先感谢您!

梅西

答案1

您必须将 .exe 和参数分开,如下所示:

#creating command
$command1 = & "C:\Program Files (x86)\CLITOOL\tool.exe" "display statistics $dateyesterdayformat $DateToday *"

这样,PowerShell 就会执行 tool.exe 并传递正确的参数。

相关内容