7

I've seen similar questions here.

One question had an accepted answer of

var spawn = require("child_process").spawn,child; child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]); child.stdout.on("data",function(data){ console.log("Powershell Data: " + data); }); child.stderr.on("data",function(data){ console.log("Powershell Errors: " + data); }); child.on("exit",function(){ console.log("Powershell Script finished"); }); child.stdin.end(); //end input 

I'm on Ubuntu so I changed it to

 var spawn = require("child_process").spawn,child; child = spawn("/usr/bin/pwsh",["/srv/webroot/pauseRS.ps1"]); child.stdout.on("data",function(data){ console.log("Powershell Data: " + data); }); child.stderr.on("data",function(data){ console.log("Powershell Errors: " + data); }); child.on("exit",function(){ console.log("Powershell Script finished"); }); child.stdin.end(); //end input 

I dont get any errors when I run the node package but it doesnt seem to be running the powershell script. Nothing is logged in the console.

The powershell script just runs a web request. When I run the powershell script by itself it runs fine and works as expected. Trying to call the powershell script with node is not giving any errors and not producing any results.

Node 12.21.0

3
  • 1
    Powershell on Ubuntu? Why? Commented Dec 18, 2021 at 19:47
  • 1
    I dont know bash and I made the script on Windows before moving to Ubuntu. Commented Dec 18, 2021 at 19:52
  • No errors and no results, but does the node process exit? As an side: It looks like child.stdin.end(); isn't required here, since you're not providing stdin input programmatically. Commented Dec 18, 2021 at 21:40

1 Answer 1

1

I tried spawning a powershell script that simply outputs Hello World! using node.js (12) in Ubuntu. Following is the code. It seems to work just fine. Can you share the content of your ps1 file?

const { spawn } = require('child_process'); const ls = spawn('/usr/bin/pwsh', ['hello.ps1']); ls.stdout.on('data', (data) => { console.log(`stdout: ${data}`); }); ls.stderr.on('data', (data) => { console.error(`stderr: ${data}`); }); ls.on('close', (code) => { console.log(`child process exited with code ${code}`); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.