从 Node.js 执行 Powershell 脚本 [英] Execute Powershell script from Node.js

查看:55
本文介绍了从 Node.js 执行 Powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been looking around the web and on Stackoverflow but hadn't found an answer to this question. How would you execute a Powershell script from Node.js? The script is on the same server as the Node.js instance.

推荐答案

你可以只产生一个子进程powershell.exe",并监听 stdout 的命令输出和 stderr 的错误:

You can just spawn a child process "powershell.exe" and listen to stdout for command output and stderr for errors:

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 执行 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆