Powershell |无法执行带空格字符的命令

时间:2014-02-10 04:08:42

标签: powershell

任何人都可以向我解释原因:

iex "C:\Program Files\test\test.exe"

返回:

  

C:\ Program:术语“C:\ Program”未被识别为cmdlet,函数,脚本文件或可操作的名称   程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。   在行:1个字符:1   + C:\ Program Files \ test \ test.exe   + ~~~~~~~~~~       + CategoryInfo:ObjectNotFound:(C:\ Program:String)[],CommandNotFoundException       + FullyQualifiedErrorId:CommandNotFoundException


我试图通过多种不同方式实现这一目标:

  • ()
  • 中包装文字
  • 将字符串放入变量并将其作为变量传递
  • 使用单引号
  • 使用双引号

我不知道如何才能意识到必须运行整个字符串,而不仅仅是第一个字。


回答后的例子

问题已得到解答。以下是我试图开展的工作:

$tool = "C:\Windows\System32\cmd.exe"
$param = "/c ping google.com -n 1"
$test = & $tool $param
Write-Host $test

事实证明,&行与此实例中的双引号""不兼容,实际上没有它们。我认为这与涉及的参数/参数有关。

1 个答案:

答案 0 :(得分:5)

&运算符与引号一起使用:

& "C:\Program Files\test\test.exe"

来自help about_operators

  & Call operator
     Runs a command, script, or script block. The call operator, also known as
     the "invocation operator," lets you run commands that are stored in
     variables and represented by strings. Because the call operator does not
     parse the command, it cannot interpret command parameters. 

         C:\PS> $c = "get-executionpolicy"
         C:\PS> $c
         get-executionpolicy

         C:\PS> & $c
         AllSigned