如何在 PowerShell 中抑制“终止批处理作业 (Y/N)”确认?

How can I suppress “Terminate batch job (Y/N)” confirmation in PowerShell?

当我在 PowerShell 中按下 Ctrl+C 时,我收到:

Terminate batch job (Y/N)?

类似于 https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation,除了 Windows PowerShell。

PowerShell 是否比 CMD 提供更多对批处理作业的控制?

行为既不是由 PowerShell 引起的,PowerShell 也不能更改它PowerShell source-code repo 不包含提示消息就是证明)。

行为内置于cmd.exe - Powershell,在这种情况下,正在调用一个.cmd文件(批处理文件),由cmd.exe解释。

  • 如果您明确控制目标可执行文件的调用,您可以通过移动到 Powershell 来解决这个问题——注意这有它自己的注意事项,见下文。

  • 如果你没有显式控制目标可执行文件的调用,那你就不走运了(除非你愿意安装第三方cmd.exe 替换)并且必须 Ctrl+C 两次 以终止执行。

  • A[n 不明智] 解决方法修改 cmd.exe 二进制文件 - 请参阅 article with instructions on how to patch the cmd.exe executable in order to suppress the prompt. Additionally, you can post a feature request on GitHub 以请求在源代码中修复此行为,尽管出于向后兼容性的原因,这不太可能发生。

演示行为:

示例假设安装了 Node.js,因此 node.exe 在您的路径中:

首先,直接调用 node.exe,需要您按 Ctrl+C 来终止进程。

PS> node -e "while (true);"

如您所见,按 Ctrl+C 立即 终止进程 - 没有确认提示。

现在,让我们创建一个调用相同命令的示例批处理文件并调用该批处理文件:

PS> "@echo off`nnode -e `"while (true);`"" | Set-Content test.cmd
PS> ./test.cmd

如您所见,按 Ctrl+C 现在会显示不需要的 Terminate batch job (Y/N)? 提示。 (如果你 运行 来自 cmd.exe 的批处理文件,你会得到相同的行为。)

证明gulp是一个cmd文件:

你说你正在 运行通过 gulp 的 CLI 命令执行你的命令。

在 Windows,gulp CLI 的入口点是 gulp.cmd [请参阅底部的更新] - 即,一个 批处理文件 . 这就是 npm-包“二进制文件”(可执行文件)实现为任一 JS 文件的一般工作方式或 shell 个脚本。

gulp调用gulp.cmd可以验证如下:

# Execute from a project folder that has `gulp` installed as a dependency.
# If `gulp` is installed *globally* 
# Note: CLI `npx` requires npm version 5.2.0+
PS C:\some\NodeJs\project> npx where gulp

您会看到如下内容:

C:\some\NodeJs\project\node_modules\.bin\gulp
C:\some\NodeJs\project\node_modules\.bin\gulp.cmd

请注意 where.exe 还列出了 无扩展名 Unix-shell 脚本,...\gulp;但是,从 cmd.exe / Powershell 这样的 shell 脚本不能直接执行,它是 ...\gulp.cmd - 批处理文件 - 即被执行。
(如果有疑问,请在 gulp.cmd 文件的开头放置诸如 @set /p dummy="Press a key" 之类的命令,您会看到当您调用 gulp 而没有 .cmd 扩展。
还要注意有没有gulp.exe.)

更一般地说,在 Windows 上,项目的 node_modules\.bin 子文件夹包含 个 CLI 入口点,用于项目所依赖的包附带的 CLI在本地:

  • node_modules\.bin\<some-cli> 是 Unix shell 脚本(其执行解释器由其 控制)。
  • node_modules\.bin\<some-cli>.cmdWindows.
  • 的辅助批处理文件

更新和未来的考虑:

在 npm 模块的上下文中,如果将 PowerShell 脚本 (*.ps1) 用作 Windows 上的帮助脚本,问题就会消失.有npmyarn等类似软件的门票可以做到这一点。也有一些缺点:

  • *.ps1 文件不能直接从 PowerShell 外部执行,特别是从 cmd.exe 和文件资源管理器(并且改变它是重要的)。

  • 从 Windows 10 开始,PowerShell 仍未完全取代 cmd.exe 作为默认 shell(并且不会很快,如果有的话) .

当从 PowerShell 调用 时,将找到一个 *.ps1 文件并且 运行 正在处理 ,所以一个可能的解决方案是npm项目提供*.ps1帮助脚本,这将优先于*.cmd 同名文件。

  • 更新
    • 最新版本的 npm(在 6.14.10 中验证)确实安装了这样的 *.ps1 文件。
    • 替代包管理器yarn, since v2 does not seem to use batch files anymore at all, so the original problem is bypassed there; (v1, by contrast, still uses batch files (only); upgrading from v1 must be done on a per-project basis see the migration instructions).

正如其他答案所指出的那样,正确的 fix 是用 ps1 版本替换 cmd 脚本。

然而,对于 Hyper shell 用户的另一种 解决方法 'Hyper yes', a plugin that automatically hits y for you 当提示出现时。

@echo off
start /w "" "C:\myfile.bat" 2>nul|findstr /i "termin"

if errorlevel 1 goto bypass


:bypass

echo hello by stexup YouTube channel!

timeout /t 5 >nul