Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Powershell script from Node.js

I've been looking around the web and on Stackoverflow but hadn't found an answer to this question. How would you execute a Powershell script from Node.js? The script is on the same server as the Node.js instance.

like image 519
Matthew Crews Avatar asked Apr 16 '12 17:04

Matthew Crews


People also ask

What is @() in PowerShell script?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.


1 Answers

You can just spawn a child process "powershell.exe" and listen to stdout for command output and stderr for errors:

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 
like image 188
muffel Avatar answered Sep 19 '22 14:09

muffel