Search code examples
node.jspowershellterminalprogress-barchild-process

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


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


Solution

  • 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).