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

查看:1203
本文介绍了从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天全站免登陆