PowerShell:如何在PowerShell中运行命令行应用程序

3
假设我有一个 Windows 命令行应用程序(abc.exe)在:C:\test 文件夹中。 我也将我的 PowerShell 脚本文件放在同一位置。
通过使用此命令,我将当前位置设置为从其中启动脚本的位置:
Set-Location $PSScriptRoot

我需要在我的脚本中运行abc.exe,这样当我按下脚本执行窗口中的Control+C时,它也会终止abc.exe
如果我在我的脚本中使用这个语法:C:\test\abc.exe,一切都是我想要的。但问题在于我不想在我的脚本中硬编码abc.exe的位置,因为用户可能会更改它。(注意:abc.exe将始终在与我的脚本相同的位置)
如果我只是使用:abc.exe,我会得到:
& : The term 'abc.exe' 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.

我该怎么办?

更新:在Windows批处理脚本中,按下Control+C将轻松终止脚本和命令行应用程序的执行。


1
请查看:https://dev59.com/mW445IYBdhLWcg3wucmo。另外请注意,在当前目录中使用“. \ abc.exe”引用命令。 - dugas
& $PSScriptRoot\abc.exe - dugas
请查看:http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx#The_Call_Operator_amp - dugas
& "$PSScriptRoot\abc.exe" 或者如果您已经在 $PSScriptRoot,那么只需使用 .\abc.exe - Bacon Bits
问题是Control+C会终止脚本而不是abc.exe。 - wiki
显示剩余4条评论
1个回答

0

你试过这个吗...

Start-Process -PSPath "C:\test\abc.exe" -WorkingDirectory "C:\test" -NoNewWindow

第二个建议:

Start-Process -PSPath "C:\test\abc.exe" -WorkingDirectory "C:\test" -NoNewWindow -Wait

我想在我的脚本中获取abc.exe的结果和交互(就像批处理脚本一样),而不是打开一个完全独立的窗口来运行abc.exe。 - wiki
请查看答案中的第二个建议。 - Buxmaniak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接