从Nodejs执行PowerShell脚本结果“运行脚本在此系统上被禁用”。

发布于 2025-01-25 18:51:18 字数 1525 浏览 2 评论 0原文

我正在尝试从节点运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆伤 2025-02-01 18:51:18

当您使用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("powershell.exe", ['-ExecutionPolicy', 'ByPass', '-file', "..\\build.ps1", "-devmode"])

至于您尝试的

在您的尝试中,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 calling
Set-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:

spawn("powershell.exe", ['-ExecutionPolicy', 'ByPass', '-file', "..\\build.ps1", "-devmode"])

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.

晨光如昨 2025-02-01 18:51:18
spawn("Powershell.exe",['-ExecutionPolicy', 'ByPass', "file/path/tmp1/test.ps1"]);
spawn("Powershell.exe",['-ExecutionPolicy', 'ByPass', "file/path/tmp1/test.ps1"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文