如何在 node.js 的子进程中捕获 PowerShell Write-Progress 文本?

How to capture PowerShell Write-Progress text in child spawn process in node.js?

在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 中捕获此流?

谢谢

Write-Progress output isn't part of PowerShell's system of output streams,因此无法捕获 - 无论是在 PowerShell 的会话中,还是通过 stdout 或 stderr 在外部捕获。

来自链接的 about_Redirection 帮助主题:

There is also a Progress stream in PowerShell, but it does not support redirection.

在 Windows 上显示了一个 解决方法 ,它不依赖于 PowerShell 的功能。

作为,进度流不可直接重定向。

也就是说,自从 Windows 1809+ 有了新的伪控制台 (ConPTY) API,它基本上允许任何程序像控制台一样工作,从而捕获直接写入的输出到控制台。

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