从 Node.js 执行 Powershell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10179114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute Powershell script from Node.js
提问by Matthew Crews
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.
我一直在浏览网络和 Stackoverflow,但没有找到这个问题的答案。您将如何从 Node.js 执行 Powershell 脚本?该脚本与 Node.js 实例位于同一服务器上。
回答by muffel
You can just spawn a child process "powershell.exe" and listen to stdout for command output and stderr for errors:
您可以只生成一个子进程“powershell.exe”并收听命令输出的标准输出和错误的标准错误:
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
回答by Julian Knight
In addition to the accepted answer, there is a Node.JS Library called Edge.jsthat allows various langugages to be executed from within Node. Including C#, J#, .Net, SQL, Python, PowerShelland other CLR languages.
除了公认的答案之外,还有一个名为Edge.js的 Node.JS 库,它允许从 Node 内部执行各种语言。包括 C#、J#、.Net、SQL、Python、PowerShell等 CLR 语言。
Note that Edge.js requires PowerShell 3.0 & only works on Windows (many of the other features work on Mac and Linux too).
请注意,Edge.js 需要 PowerShell 3.0 并且仅适用于 Windows(许多其他功能也适用于 Mac 和 Linux)。
回答by Ran Cohen
Or you can just use Node-PowerShell.
或者你可以只使用Node-PowerShell。
Node-PowerShell taking advantage of two of the simplest, effective and easy tools that exist in the today technology world. On the one hand, NodeJS which made a revolution in the world of javascript, and on the other hand, PowerShell which recently came out with an initial open-source, cross-platform version, and by connecting them together, gives you the power to create any solution you were asked to, no matter if you are a programmer, an IT or a DevOps guy.
Node-PowerShell 利用当今技术世界中存在的两个最简单、有效和简单的工具。一方面,NodeJS 在 JavaScript 世界中掀起了一场革命,另一方面,PowerShell 最近推出了一个初始的开源跨平台版本,通过将它们连接在一起,让您能够创建您被要求的任何解决方案,无论您是程序员、IT 人员还是 DevOps 人员。
回答by Javier Castro
This option works for me, when the script is not already there, but you want to generate some commands dynamically, send them, and work with the results back on node.
这个选项对我有用,当脚本不存在时,但你想动态生成一些命令,发送它们,并在节点上处理结果。
var PSRunner = {
send: function(commands) {
var self = this;
var results = [];
var spawn = require("child_process").spawn;
var child = spawn("powershell.exe", ["-Command", "-"]);
child.stdout.on("data", function(data) {
self.out.push(data.toString());
});
child.stderr.on("data", function(data) {
self.err.push(data.toString());
});
commands.forEach(function(cmd){
self.out = [];
self.err = [];
child.stdin.write(cmd+ '\n');
results.push({command: cmd, output: self.out, errors: self.err});
});
child.stdin.end();
return results;
},
};
module.exports = PSRunner;
回答by Honest Objections
The newer way to do this
执行此操作的较新方法
const { exec } = require('child_process');
exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
// do whatever with stdout
})

