I'm attempting to run a powershell script from node. Here is my current code:
try {
var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe", ['-file', "..\\build.ps1", "-devmode"])
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
} catch (error) {
console.error(error);
}
The code is based on this question. When I run it, I get the following error:
Powershell Errors: File C:\<path>\build.ps1 cannot be loaded because running scripts is disabled on this system. For more information,
see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess
I have tried setting the execution policy Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope LocalMachine
and I also tried just bypassing it when calling the script, like this:
child = spawn("powershell.exe", ['-ExecutionPolicy ByPass', '-file', "..\\build.ps1", "-devmode"])
This just resulted in The term '-ExecutionPolicy' is not recognized as the name of a cmdlet, function, script file, or operable program.
So how can I execute this script from node without execution policy errors?