powershell - 在 PowerShell 中运行外部二进制文件会在新的 cmd 窗口中打开进程,但仅在不使用相对路径时
问题描述
如果我cd
进入一个包含可执行二进制文件的文件夹并在 PowerShell 中键入它的名称,它会将标准输出返回到 PowerShell。
PS C:\Users\User\AppData\Roaming\npm> mybinary
这不会打开 cmd 窗口并在 PowerShell 窗口内执行。
但是,如果我尝试从其他地方调用,当我不在二进制文件所在的目录中时,它会创建一个新的 cmd 窗口并在那里运行。为什么?我想在现有的 PowerShell 窗口中运行它。
PS C:\Users\User> & "C:\Users\User\AppData\Roaming\npm\mybinary"
打开一个新的 cmd 窗口并在那里运行。
解决方案
你从哪里运行这个?控制台主机/ISE/VSCode 其他?
PowerShell 正在按照您的要求执行。可执行文件有什么作用?你期望得到什么?尝试使用任何其他内置 Windows 可执行文件的用例。
# Examples: all of which send results back to the PowerShell console without popping a new window.
PS C:\Scripts> & "C:\Windows\system32\nslookup.exe"
# Results
<#
Default Server: L...
Address: 172...
#>
PS C:\Scripts> nslookup stackoverflow.com
# Results
<#
Server: L...
Address: 172...1
Non-authoritative answer:
Name: stackoverflow.com
Addresses: 151...
#>
PS C:\Scripts> & 'nslookup' 'stackoverflow.com'
# Results
<#
Server: L...
Address: 172...
Non-authoritative answer:
Name: stackoverflow.com
Addresses: 151...
#>
PS C:\Scripts> & "nslookup" "stackoverflow.com"
# Results
<#
Server: L...
Address: 172...
Non-authoritative answer:
Name: stackoverflow.com
Addresses: 151....
#>
PS C:\Scripts> & "nslookup stackoverflow.com"
# Results
<#
& : The term 'nslookup stackoverflow.com' 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.
#>
PowerShell 不运行可执行文件,而 cmd.exe 可以。请注意,Powershell_ISE 主动阻止交互式可执行文件
PowerShell ISE 限制 (Windows) | 微软文档
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/mt708811(v=vs.85)
$psUnsupportedConsoleApplications
# Results
<#
wmic
wmic.exe
cmd
cmd.exe
diskpart
diskpart.exe
edit.com
netsh
netsh.exe
nslookup
nslookup.exe
powershell
powershell.exe
#>
在 PowerShell 控制台中运行可执行文件,只要您传递它所需的所有内容,在后台调用 cmd.exe,cmd.exe 运行可执行文件,并将 STDOUT 返回到调用控制台。
参考:
• PowerShell:运行可执行文件
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
- 呼叫接线员
&
原因:用于将字符串视为 SINGLE 命令。对于处理空格很有用。
在 PowerShell V2.0 中,如果您正在运行 7z.exe (7-Zip.exe) 或其他以数字开头的命令,则必须使用命令调用运算符 &。
PowerShell V3.0 解析器现在更智能了,在这种情况下,您不再需要 &。
详细信息:运行命令、脚本或脚本块。调用运算符,也称为“调用运算符”,可让您运行存储在变量中并由字符串表示的命令。因为调用操作符不解析命令,所以不能解释命令参数
# Example:
& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen
当外部命令有很多参数或者参数或路径中有空格时,事情会变得很棘手!
使用空格,您必须嵌套引号,结果并不总是很清楚!
在这种情况下,最好像这样分开所有内容:
$CMD = 'SuperApp.exe'
$arg1 = 'filename1'
$arg2 = '-someswitch'
$arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
$arg4 = '-yetanotherswitch'
& $CMD $arg1 $arg2 $arg3 $arg4
# or same like that:
$AllArgs = @('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs
通过 VSCode,您可以获得不同的响应,具体取决于您使用的是 VSCode 的集成控制台(类似 ISE 的环境)还是控制台主机(普通的 PowerShell 控制台)。
推荐阅读
- r - 通过使用 FOR 循环删除缺失值,查找 R 数据集中列的平均值
- laravel - 如何在 PhpStorm 中更改自定义 Laravel 指令的颜色?
- python - 如何在 folium choropleths 中正确使用 key_on
- javascript - Yarn Start 无法生成 CRKA 项目
- sql - 查询结果到 temp,然后移动到 mast 表
- batch-file - 如何创建可以从一组存根创建 .jar 文件的批处理脚本?
- node.js - EventStreams (SSE) - 向客户端广播更新。可能吗?
- html - 在css中如何在一个div之间居中一个div而不触及父div的属性,只改变子div的属性
- recommendation-engine - 什么时候使用用户-用户协同过滤,什么时候使用Item-Item协同过滤?
- woocommerce - 如何编写一个 foreach 循环来检查用户在 Woocommerce 中购买特定商品的次数?