javascript execute powershell script 1

javascript execute powershell script

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

Here is what the above code is Doing:
1. We’re using the spawn function to run the powershell.exe program.
2. We’re passing the path to the script we want to run as the second argument.
3. We’re listening for the data event on the child process’s stdout stream.
4. We’re listening for the data event on the child process’s stderr stream.
5. We’re listening for the exit event on the child process.
6. We’re calling the end function on the child process’s stdin stream.

Similar Posts