2011-01-09 168 views
68

我想使用PowerShell脚本执行EXE文件。如果我使用命令行它的工作原理没有问题(第一我公司供应的可执行文件和一系列参数的名称来调用它):使用PowerShell脚本执行EXE文件

"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode 

但做一个脚本里面同样的事情会返回一个错误:

The term '"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

(我调用使用 “&” 操作命令。)

我怎样才能解决这个问题呢?

+3

请发布您的代码。由于这是问题所在,因此让它看起来比猜测容易得多。 – 2011-01-09 15:14:49

回答

79
& "C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode

[System.Diagnostics.Process]::Start("C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe", "C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode")

UPDATE:对不起,我错过了 “(我调用使用命令” & “运算符)” 的句子。我在动态评估路径时遇到了这个问题。尝试调用 - 表达式构造:

Invoke-Expression "& `"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe`" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode"
+0

奇怪的是,原始问题中的命令几乎完全适用于PS2.0中的虚拟机。但是同样的命令现在在PS4.0系统上失败了。有趣的是,我也叫TestExecute。我想我最喜欢选项2,我会给你一个镜头,谢谢! – EJA 2014-06-25 18:37:28

22

它看起来像你指定EXE和它的第一个参数在一个单一的字符串例如; '"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode'。这不起作用。一般来说,你调用具有像这样在其路径中有空格的本地命令:

& "c:\some path with spaces\foo.exe" <arguments go here> 

也就是说&预计应遵循的标识命令的字符串:cmdlet,函数,本地EXE相对或绝对路径。

一旦你得到的只是这个工作:

& "c:\some path with spaces\foo.exe" 

开始上的参数报价为必要的工作。虽然看起来你的参数应该很好(没有空格,没有其他特殊字符由PowerShell解释)。

+1

无论如何,如果脚本和exe文件位于同一个文件夹中,则无需执行完整路径的powershell脚本?或者是否有可能使用相对路径? – guanome 2012-09-28 21:38:54

-1

术语“greyhound.exe未被识别为cmdlet,函数,脚本文件或可操作的程序的名称...

但是,如果我运行它作为一个可信任的命令。\ greyhound.exe那么可执行文件运行。