简体   繁体   English

使用 PowerShell 脚本执行 EXE 文件

[英]Executing an EXE file using a PowerShell script

I'm trying to execute an EXE file using a PowerShell script.我正在尝试使用 PowerShell 脚本执行 EXE 文件。 If I use the command line it works without a problem (first I supply the name of the executable and series of parameters to invoke it):如果我使用命令行,它可以正常工作(首先我提供可执行文件的名称和一系列参数来调用它):

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

But doing the exact same thing inside of a script returns an error:但是在脚本中执行完全相同的操作会返回错误:

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.术语'"C:\\Program Files\\Automated QA\\TestExecute 8\\Bin\\TestExecute.exe" C:\\temp\\TestProject1\\TestProject1.pjs /run /exit /SilentMode'未被识别为 cmdlet、函数的名称、脚本文件或可运行的程序。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

(I invoked the command using the "&" operator.) (我使用“&”运算符调用了该命令。)

How can I fix this problem?我该如何解决这个问题?

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

or或者

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

UPDATE: sorry I missed "(I invoked the command using the "&" operator)" sentence.更新:抱歉,我错过了“(我使用“&”运算符调用命令)”句子。 I had this problem when I was evaluating the path dynamically.我在动态评估路径时遇到了这个问题。 Try Invoke-Expression construction:尝试调用表达式构造:

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

It looks like you're specifying both the EXE and its first argument in a single string eg;看起来您在单个字符串中同时指定了 EXE 及其第一个参数,例如; '"C:\\Program Files\\Automated QA\\TestExecute 8\\Bin\\TestExecute.exe" C:\\temp\\TestProject1\\TestProject1.pjs /run /exit /SilentMode' . '"C:\\Program Files\\Automated QA\\TestExecute 8\\Bin\\TestExecute.exe" C:\\temp\\TestProject1\\TestProject1.pjs /run /exit /SilentMode' This won't work.这行不通。 In general you invoke a native command that has a space in its path like so:通常,您调用一个在其路径中有一个空格的本机命令,如下所示:

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

That is & expects to be followed by a string that identifies a command: cmdlet, function, native exe relative or absolute path.&期望后跟标识命令的字符串:cmdlet、函数、本机 exe 相对或绝对路径。

Once you get just this working:一旦你开始工作:

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

Start working on quoting of the arguments as necessary.根据需要开始引用参数。 Although it looks like your arguments should be just fine (no spaces, no other special characters interpreted by PowerShell).虽然看起来你的参数应该没问题(没有空格,没有其他由 PowerShell 解释的特殊字符)。

In the Powershell, cd to the .exe file location.在 Powershell 中,cd 到 .exe 文件位置。 For example:例如:

cd C:\\Users\\Administrators\\Downloads cd C:\\用户\\管理员\\下载

PS C:\\Users\\Administrators\\Downloads> & '.\\aaa.exe' PS C:\\Users\\Administrators\\Downloads> & '.\\aaa.exe'

The installer pops up and follow the instruction on the screen.安装程序会弹出并按照屏幕上的说明进行操作。

  1. clone $args克隆 $args
  2. push your args in new array将你的参数推入新数组
  3. & $path $args & $path $args

Demo:演示:

$exePath = $env:NGINX_HOME + '/nginx.exe'
$myArgs = $args.Clone()

$myArgs += '-p'
$myArgs += $env:NGINX_HOME

& $exepath $myArgs

Not being a developer I found a solution in running multiple ps commands in one line.不是开发人员,我找到了在一行中运行多个 ps 命令的解决方案。 Eg:例如:

powershell "& 'c:\path with spaces\to\executable.exe' -arguments ; second command ; etc

By placing a " (double quote) before the & (ampersand) it executes the executable. In none of the examples I have found this was mentioned. Without the double quotes the ps prompt opens and waits for input.通过在 &(与号)之前放置一个 "(双引号),它会执行可执行文件。在我发现的所有示例中都没有提到这一点。没有双引号,ps 提示会打开并等待输入。

Here is a variation on some of the previous responses.以下是之前一些回复的变体。 Either of the below options can be added to a Powershell profile to suit your needs.可以将以下任一选项添加到 Powershell 配置文件中以满足您的需求。

Use an alias使用别名

If you just want to run something without mucking up $env:PATH .如果你只想运行一些东西而不破坏$env:PATH

Set-Alias -Name docker -Value 'C:\Program Files\Docker\Docker\resources\bin\docker.exe'

Use a function使用函数

Or if you need more scripting.或者,如果您需要更多脚本。

function docker {
# below is a basic example - go crazy here
& 'C:\Program Files\Docker\Docker\resources\bin\docker.exe' @Args
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM