Mat*_*ews 32 powershell node.js
我一直在寻找网络和Stackoverflow,但没有找到这个问题的答案.你会如何从Node.js执行Powershell脚本?该脚本与Node.js实例位于同一服务器上.
muf*_*fel 63
你可以只生成子进程"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
Run Code Online (Sandbox Code Playgroud)
Hon*_*ons 17
执行此操作的较新方法
const { exec } = require('child_process');
exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
// do whatever with stdout
})
Run Code Online (Sandbox Code Playgroud)
Jul*_*ght 11
除了接受的答案之外,还有一个名为Edge.js的Node.JS库,它允许从Node内执行各种语言.包括C#,J#,.Net,SQL,Python,PowerShell和其他CLR语言.
请注意,Edge.js需要PowerShell 3.0才能在Windows上运行(许多其他功能也适用于Mac和Linux).
Ran*_*hen 10
或者您可以使用Node-PowerShell.
Node-PowerShell利用了当今技术领域中存在的两种最简单,最有效和最简单的工具.一方面,NodeJS在javascript世界中取得了一场革命,另一方面,PowerShell最近推出了最初的开源,跨平台版本,并通过将它们连接在一起,使您有能力无论您是程序员,IT人员还是DevOps人员,都可以创建您被要求的任何解决方案.
小智 9
当脚本不存在时,此选项适用于我,但您希望动态生成一些命令,发送它们,并在节点上使用结果.
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42400 次 |
| 最近记录: |