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

Viewed 412

In node.js I create child processes like this for example

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);

However, in the child process, it displays a progress bar like this picture (I got this from running it manually from a powershell terminal). The area on top with the light blue background is static and stays on top, while the text in it updates.

enter image description here

However this progress text doesn't get captured in stdout or stderr. How can I capture this stream in node.js?

Thanks

2 Answers

As mklement0 stated, the progress stream is not directly redirectable.

That being said, since Windows 1809+ there is the new pseudo console (ConPTY) API, which basically allows any program to act like the console, thus capturing output that is written directly to the console.

Doing a quick search, there is the node-pty module which supposedly utilizes the ConPTY API on Windows. I haven't tried it but it looks promising (VS Code is using it for its integrated terminal).

Write-Progress output isn't part of PowerShell's system of output streams, so it cannot be captured - neither in-session from PowerShell, nor externally via stdout or stderr.

From the linked about_Redirection help topic:

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

zett42's helpful answer shows a workaround on Windows that doesn't rely on PowerShell's features.

Related