繁体   English   中英

如何使用Express将Powershell命令结果推送到NodeJ中的json响应?

[英]How to push powershell command result to json response in NodeJs using Express?

如何处理这个任务?

我需要来自Powershell.on事件的推送数据,以将一些数据响应到客户端网页。 但是我不是一个好的程序员。 怎么做?:

router.all('/', (req, res, next) => {
    var NetbiosName = req.query.hostname.split('.', 1);
    var cmd = "New-ADComputer -Name " +  NetbiosName[0] + " -SamAccountName " + 
    NetbiosName[0]; 
    var gdata;
    let ps = new PowerShell(cmd);
    ps.on("error", err => {
      console.error(err);
    });
    ps.on("output", data => {
      //need to push this data to responce json
      console.log(data);
    });
    ps.on("error-output", data => {
      //need to push this data to responce json
      console.error(data);
    });
    ps.on("end", code => {
    });

    //console.log(ps.send(['help',]));
    res.status(200).json({
      "hostname": req.query.hostname,
      "userclass": req.query.userclass,
      "host": NetbiosName[0],
      "cmd": cmd,
      "data": "Data from ps.on",
    });
    //console.log(util.inspect(req));
});
  1. 编写一个函数以将数据发送回客户端。

     function sendResponse(data) { res.status(200).json({ "hostname": req.query.hostname, "userclass": req.query.userclass, "host": NetbiosName[0], "cmd": cmd, "data": data }); } 
  2. 从声明的事件输出错误输出中调用该函数

     ps.on("output", data => { sendResponse(data); }); ps.on("error-output", data => { sendResponse(data); }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM