我在网上和Stackoverflow上找了很久,但没有找到这个问题的答案。您将如何从Node.js执行Powershell脚本?该脚本与Node.js实例位于同一服务器上。
发布于 2012-04-17 04:49:01
您可以只生成一个子进程"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
发布于 2020-04-15 09:24:27
做这件事的新方法
const { exec } = require('child_process');
exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
// do whatever with stdout
})
发布于 2014-07-10 00:29:21
除了公认的答案之外,还有一个名为Edge.js的Node.JS库,它允许在节点内执行各种语言。包括C#,J#,.Net,SQL,Python,PowerShell和其他CLR语言。
请注意,Edge.js需要Windows3.0&只能在PowerShell上运行(许多其他功能也可以在Mac和Linux上运行)。
https://stackoverflow.com/questions/10179114
复制相似问题