【问题标题】:How to capture PowerShell Write-Progress text in child spawn process in node.js?如何在 node.js 的子生成过程中捕获 PowerShell 写入进度文本?
【发布时间】:2021-03-07 20:02:12
【问题描述】:

例如,在 node.js 中,我创建了这样的子进程

child_process.spawn('powershell.exe', ['-executionpolicy', 'remotesigned', '-File', 'save_site_login_web.ps1', data.app_catalogue_url, data.ids.join(",")], { windowsHide:true });

const handler = (data) => {
     // send data to client
};
    
watcher_shell.stdout.on('data', handler);
watcher_shell.stderr.on('data', handler);

但是,在子进程中,它会显示一个类似这张图片的进度条(我是通过从 powershell 终端手动运行它得到的)。顶部带有浅蓝色背景的区域是静态的并保持在顶部,而其中的文本会更新。

但是,此进度文本不会在 stdout 或 stderr 中捕获。如何在 node.js 中捕获此流?

谢谢

【问题讨论】:

    标签: node.js powershell terminal progress-bar child-process


    【解决方案1】:

    作为mklement0 stated,进度流不能直接重定向。

    话虽如此,从 Windows 1809+ 开始,有了新的伪控制台 (ConPTY) API,它基本上允许任何程序像控制台一样工作,从而捕获直接写入控制台的输出。

    快速搜索,有一个node-pty 模块,据说它在 Windows 上使用了 ConPTY API。我还没有尝试过,但它看起来很有希望(VS Code 正在将它用于其集成终端)。

    【讨论】:

    • 这确实捕获了进度流。我使用 npm 模块 ansi-to-html 将其转换为友好的 HTML,但在使用 node-pty 时似乎效果不佳。你知道这个是否有另一个模块吗?
    • @omega 我不知道。这只是“node conpty”的第一个搜索结果。
    【解决方案2】:

    Write-Progress 输出不是PowerShell's system of output streams 的一部分,因此无法捕获 - 既不是来自 PowerShell 的会话中,也不是外部通过 stdout 或 stderr。

    来自链接的about_Redirection帮助主题:

    PowerShell中也有Progress流,但不支持重定向。

    zett42's helpful answer 在不依赖 PowerShell 功能的 Windows 上显示了一个解决方法

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2018-07-23
      相关资源
      最近更新 更多