小编Jab*_*874的帖子

在节点js中运行powershell脚本?

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

一个问题的公认答案是

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

javascript powershell node.js

7
推荐指数
1
解决办法
749
查看次数

标签 统计

javascript ×1

node.js ×1

powershell ×1