【发布时间】:2022-11-10 00:55:19
【问题描述】:
我正在尝试从节点运行 powershell 脚本。这是我当前的代码:
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);
}
代码基于this question。当我运行它时,我收到以下错误:
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
我尝试设置执行策略Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope LocalMachine,并且在调用脚本时也尝试绕过它,如下所示:
child = spawn("powershell.exe", ['-ExecutionPolicy ByPass', '-file', "..\\build.ps1", "-devmode"])
这只是导致The term '-ExecutionPolicy' is not recognized as the name of a cmdlet, function, script file, or operable program.
那么如何从节点执行此脚本而不会出现执行策略错误?
【问题讨论】:
标签: node.js powershell command-line-interface executionpolicy