从 NodeJS 执行 PowerShell 脚本结果 "running scripts is disabled on this system"
Execute PowerShell script from NodeJS results in "running scripts is disabled on this system"
我正在尝试 运行 来自节点的 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.
那么如何从节点执行这个脚本而不出现执行策略错误?
当您使用 Windows PowerShell CLI(powershell.exe
) with the -File
parameter to directly invoke a .ps1
script file, using the -ExecutionPolicy
CLI parameter is indeed the only effective way to override the script-file execution policy 有效 临时,仅对于给定进程:-ExecutionPolicy Bypass
等同于从 PowerShell 会话内部调用
Set-ExecutionPolicy Bypass -Scope Process -Force
。
但是,要从 spawn
调用执行此操作,每个参数 - 无论它代表参数 name 还是 value - 必须作为自己的数组元素传递:
spawn("powershell.exe", ['-ExecutionPolicy', 'ByPass', '-file', "..\build.ps1", "-devmode"])
至于你试过的:
在您的尝试中,spawn
必须在生成的命令行(在 Windows 上)的 "..."
中包含 '-ExecutionPolicy Bypass'
,因为它包含空格。
当 powershell.exe
解析生成的命令行时,它 不会 将
"-ExecutionPolicy Bypass"
识别为试图通过 -ExecutionPolicy
带值的参数;事实上,它根本不将此参数识别为 CLI 参数,因此默认将其视为 PowerShell 源代码 - 即好像它已传递给 默认 CLI 参数,-Command
(-c
).
也就是说,实际上您的尝试等同于从 PowerShell 会话内部提交 -ExecutionPolicy Bypass ...
作为命令,这会导致您看到的错误。
我正在尝试 运行 来自节点的 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.
那么如何从节点执行这个脚本而不出现执行策略错误?
当您使用 Windows PowerShell CLI(powershell.exe
) with the -File
parameter to directly invoke a .ps1
script file, using the -ExecutionPolicy
CLI parameter is indeed the only effective way to override the script-file execution policy 有效 临时,仅对于给定进程:-ExecutionPolicy Bypass
等同于从 PowerShell 会话内部调用 Set-ExecutionPolicy Bypass -Scope Process -Force
。
但是,要从 spawn
调用执行此操作,每个参数 - 无论它代表参数 name 还是 value - 必须作为自己的数组元素传递:
spawn("powershell.exe", ['-ExecutionPolicy', 'ByPass', '-file', "..\build.ps1", "-devmode"])
至于你试过的:
在您的尝试中,spawn
必须在生成的命令行(在 Windows 上)的 "..."
中包含 '-ExecutionPolicy Bypass'
,因为它包含空格。
当 powershell.exe
解析生成的命令行时,它 不会 将 "-ExecutionPolicy Bypass"
识别为试图通过 -ExecutionPolicy
带值的参数;事实上,它根本不将此参数识别为 CLI 参数,因此默认将其视为 PowerShell 源代码 - 即好像它已传递给 默认 CLI 参数,-Command
(-c
).
也就是说,实际上您的尝试等同于从 PowerShell 会话内部提交 -ExecutionPolicy Bypass ...
作为命令,这会导致您看到的错误。