从Nodejs执行PowerShell脚本结果“运行脚本在此系统上被禁用”。
我正在尝试从节点运行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);
}
该代码基于这个问题。运行它时,我会收到以下错误:
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 -execution -execution -execution -ececution -ececution -ececution -ecection -ecection -scope localmachine
,我也尝试在调用脚本时绕过它,如这样:this:
child = spawn("powershell.exe", ['-ExecutionPolicy ByPass', '-file', "..\\build.ps1", "-devmode"])
this :this刚刚导致术语“ -ececution-policy”不被识别为cmdlet,函数,脚本文件或可操作程序的名称。
因此,我如何在没有执行策略错误的情况下从节点执行此脚本?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用Windows PowerShell CLI (
powershell.exe
),带有-file
参数直接调用.ps1
脚本文件 ,使用-execution-ecution-policy
cli参数确实是 覆盖脚本文件<<>的唯一有效方法< a href =“ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about/about/about_execution_policies” rel =“ nofollow noreflow noreferrer” > Ad Hoc ,仅对于给定的过程:-ExecutionPolicy旁路
等同于调用set -execution -execution -execution -ececution -ececution -ececution -ececution -ececution -ececution -ececution -ecectionpolicy bypass -scope process -force 从内部的powershell会话。
但是,要从
spawn
调用,每个参数 - 不考虑其表示参数 name 或 value - 必须为传递作为其自己的数组元素 :至于您尝试的:
在您的尝试中,
Spawn
必要性包含' -ExecutionPolicy Bypass'
in“ ...”
在结果命令行(在Windows上)上,鉴于它包含空格。当
powershell.exe
解析结果命令行时,它确实不是识别“ - executionpolicy bypass”
作为尝试传递<代码> -ExecutionPolicy 带有值的参数;实际上,它根本不将此参数识别为CLI参数,因此默认将其视为PowerShell源代码 - IE,就好像已传递给 default cli参数,-command
(-c
)。也就是说,实际上,您的尝试是在提交
-ExecutionPolicy旁路...
作为命令中的命令,这导致您看到的错误。When you're using the 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 in effect ad hoc, for the given process only:-ExecutionPolicy Bypass
is equivalent to callingSet-ExecutionPolicy Bypass -Scope Process -Force
from inside a PowerShell session.However, to do so from a
spawn
call, each argument - irrespective of whether it represent a parameter name or value - must be passed as its own array element:As for what you tried:
In your attempt,
spawn
of necessity encloses'-ExecutionPolicy Bypass'
in"..."
on the resulting command line (on Windows), given that it contains spaces.When
powershell.exe
parses the resulting command line, it does not recognize"-ExecutionPolicy Bypass"
as an attempt to pass the-ExecutionPolicy
parameter with a value; in fact, it doesn't recognize this argument as a CLI parameter at all, and therefore defaults to treating it as PowerShell source code - i.e. as if it had been passed to the default CLI parameter,-Command
(-c
).That is, in effect your attempt was tantamount to submitting
-ExecutionPolicy Bypass ...
as a command from inside a PowerShell session, which results in the error you saw.