在节点js中运行powershell脚本?

Jab*_*874 7 javascript powershell node.js

我在这里看到过类似的问题。

一个问题的公认答案是

var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]);
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

Run Code Online (Sandbox Code Playgroud)

我使用的是 Ubuntu,所以我将其更改为

        var spawn = require("child_process").spawn,child;
            child = spawn("/usr/bin/pwsh",["/srv/webroot/pauseRS.ps1"]);
            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
Run Code Online (Sandbox Code Playgroud)

当我运行节点包时,我没有收到任何错误,但它似乎没有运行 powershell 脚本。控制台中没有记录任何内容。

powershell 脚本仅运行一个 Web 请求。当我单独运行 powershell 脚本时,它运行良好并且按预期工作。尝试使用节点调用 powershell 脚本不会给出任何错误,也不会产生任何结果。

节点12.21.0

dna*_*ion 1

我尝试生成一个 powershell 脚本,该脚本仅Hello World!在 Ubuntu 中使用 node.js (12) 进行输出。以下是代码。看起来效果很好。可以分享一下你的ps1文件的内容吗?

const { spawn } = require('child_process');

const ls = spawn('/usr/bin/pwsh', ['hello.ps1']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});
Run Code Online (Sandbox Code Playgroud)