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

2024-04-11

When I press Ctrl+C in PowerShell, I receive:

终止批处理作业(是/否)?

如同https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation,Windows PowerShell 除外。

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


tl;dr

  • The cause问题在于gulp(以及显示此消息的任何其他命令)实际上是批处理文件 (*.cmd, *.bat)行为是内置于cmd.exe,批处理文件的解释器。

  • 包管理器越来越多地提供电源外壳脚本(*.ps1)(请参阅底部部分),这可以绕过从 PowerShell 运行时的问题。


The 行为既不是由 PowerShell 引起的,PowerShell 也无法更改它(证明了不包含提示信息)。

The 行为是内置于cmd.exe- 在本例中,Powershell 正在调用.cmd文件(批处理文件),其解释为cmd.exe.

  • 如果您显式控制目标可执行文件的调用,则可以通过移动到 Powershell 来修复此问题- 请注意,这有其自身的考虑因素,请参见下文。

  • If you do not explicitly control the invocation of the target executable, you're out of luck (unless you're willing to install third-party cmd.exe replacements) and must press Ctrl+C twice in order to terminate execution.

  • A[不明智]解决方法 is to 修改cmd.exe binary - see 文章包含有关如何修补的说明cmd.exe可执行文件以抑制提示 https://itsme.home.xs4all.nl/projects/misc/patching-cmdexe.html。此外,您可以在以下位置发布功能请求:GitHub https://github.com/Microsoft/console请求从源头修复此行为,尽管出于向后兼容性的原因这不太可能发生。

演示该行为:

这些示例假设已安装 Node.js 并且node.exe因此在你的路径中:

First, invoke node.exe directly, with a tight loop that requires you to press Ctrl+C to terminate the process.

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

As you'll see, pressing Ctrl+C instantly terminates the process - no confirmation prompt.

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

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

As you'll see, pressing Ctrl+C now presents the undesired Terminate batch job (Y/N)? prompt. (You'd get the same behavior if you ran the batch file from cmd.exe.)

为了证明gulp is a .cmd(批处理)文件:

你说你正在通过以下方式运行你的命令gulp https://www.npmjs.com/package/gulp's CLI.

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

That 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- 外壳脚本,...\gulp;然而,从cmd.exe/powershell这样的shell脚本是不能直接执行的,它是...\gulp.cmd - the 批处理文件- 已执行。
(如果有疑问,请输入命令,例如@set /p dummy="Press a key"在开始时gulp.cmd文件,您将看到当您调用时会执行此命令gulp没有.cmd扩大。
另请注意,有no gulp.exe.)

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

  • node_modules\.bin\<some-cli>是 Unix shell 脚本(其执行解释器通过其控制舍邦线 https://stackoverflow.com/a/33510581/45375).
  • node_modules\.bin\<some-cli>.cmd是辅助批处理文件Windows.

更新和未来考虑:

在 npm 模块的上下文中,如果电源外壳脚本 (*.ps1) 被用作 Windows 上的帮助程序脚本。有门票可以去npm, yarn和类似的软件来做到这一点。也有一些缺点:

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

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

被叫时来自 PowerShell, a *.ps1文件将被找到并在进程中运行, so 一个可能的解决方案是npm项目到also提供*.ps1帮助脚本,这将优先于*.cmd同名的文件。

  • Update:
    • 最新版本npm(在 6.14.10 中验证)确实要安装这样的*.ps1 files.
    • 替代包管理器yarn https://yarnpkg.com/, since v2似乎根本不再使用批处理文件,所以原来的问题是bypassed there; (v1相比之下,仍然使用批处理文件(仅);从 v1 升级必须在每个项目基础见迁移说明 https://yarnpkg.com/getting-started/migration).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 PowerShell 中取消“终止批处理作业 (Y/N)”确认? 的相关文章

随机推荐