node.js - 从 Node.js 执行 Powershell 脚本

标签 node.js powershell

我一直在浏览网络和 Stackoverflow,但没有找到这个问题的答案。您将如何从 Node.js 执行 Powershell 脚本?该脚本与 Node.js 实例位于同一服务器上。

最佳答案

您可以只生成一个子进程“powershell.exe”并监听 stdout 的命令输出和 stderr 的错误:

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

关于node.js - 从 Node.js 执行 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10179114/

相关文章:

mysql - 通过 powershell 安装 Mysql.msi 时出现以下错误

powershell - 将 "Visual Studio code"与交互式 Powershell 窗口(如 Powershell ISE)结合使用

c++ - 使 system() 产生的子进程在父进程收到终止信号并退出后继续运行

javascript - 将 api.ai 聊天机器人添加到我的 Meteor.js 聊天应用程序

multithreading - 在 powershell 中使用多线程一次调用多个 URL

powershell - 如何在Powershell中使用While循环?

powershell - 如何在 Windows 事件日志中存储对象?

javascript - 在 IDEA 中仅按模式将 ESLint 规则应用于某些文件

javascript - localhost 不输出我使用的路由器和 Controller 的 JSON

node.js - 带有 Mongoose 和数组的聚合框架