简体   繁体   English

从 NodeJS 执行 PowerShell 脚本会导致“在此系统上禁用运行脚本”

[英]Execute PowerShell script from NodeJS results in "running scripts is disabled on this system"

I'm attempting to run a powershell script from node.我正在尝试从节点运行 powershell 脚本。 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:我尝试设置执行策略Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope LocalMachine并且我也尝试在调用脚本时绕过它,如下所示:

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.这只是导致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?那么如何从节点执行此脚本而不会出现执行策略错误?

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当您使用带有 -File 参数的 Windows PowerShell CLI ( powershell.exe ) -File调用.ps1脚本文件时,使用-ExecutionPolicy脚本参数执行策略确实有效ad hoc ,仅针对给定进程: -ExecutionPolicy Bypass等效于调用
Set-ExecutionPolicy Bypass -Scope Process -Force from inside a PowerShell session. Set-ExecutionPolicy Bypass -Scope Process -Force从 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调用中执行此操作,每个参数 - 无论它表示参数名称还是- 都必须作为其自己的数组元素传递

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.在您的尝试中, spawn必须在生成的命令行(在 Windows 上)的"..."中包含'-ExecutionPolicy Bypass' ,因为它包含空格。

When powershell.exe parses the resulting command line, it does not recognize powershell.exe解析生成的命令行时,无法识别
"-ExecutionPolicy Bypass" as an attempt to pass the -ExecutionPolicy parameter with a value; "-ExecutionPolicy Bypass"作为尝试将-ExecutionPolicy参数传递给一个值; in fact, it doesn't recognize this argument as a CLI parameter at all, and therefore defaults to treating it as PowerShell source code - ie as if it had been passed to the default CLI parameter, -Command ( -c ).事实上,它根本不将此参数识别为 CLI 参数,因此默认将其视为PowerShell 源代码- 即好像它已传递给默认CLI 参数-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.也就是说,实际上您的尝试等同于提交-ExecutionPolicy Bypass...作为来自 PowerShell session 内部的命令,这会导致您看到的错误。

spawn("Powershell.exe",['-ExecutionPolicy', 'ByPass', "file/path/tmp1/test.ps1"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM