2

I wrote a node JS script, that should execute using the PowerShell command line. And then return the PowerShell response to angularjs. The PowerShell command takes more than 5 minutes for execution. So node is sending an empty response to angularjs. For this, I tried the callback function. But I'm getting the same empty response. Please help me to solve this.

This is code for executing powershell using callback function in node js

let ps = new shell({
        executionPolicy: 'Bypass',
        noProfile: true
    });

function getData(callback){
    ps.addCommand('C:/Users/sowmi096/Desktop/Code/jobid.ps1',a)
    ps.invoke()
    .then(output => {
        return callback(null,output);
    })
    .catch(err => {
        return callback(null,err);
        ps.dispose();
    });
}
getData(function onGetData(err,data){
    if(err)
        res.send(err);
    res.send(data);
});
mklement0
  • 382,024
  • 64
  • 607
  • 775
Sowmiya B
  • 39
  • 1
  • 6

2 Answers2

0

A very similar question was asked here:

Execute Powershell script from Node.js

Someone there suggested the Edge.js library. It allows various languages to be executed from within Node. Including C#, J#, .Net, SQL, Python, PowerShell and other CLR languages. Note that Edge.js requires PowerShell 3.0 & only works on Windows, but many of the other features work on Mac and Linux too.

There's also a code sample there by Javier Castro that returns results from Powershell.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • The OP is looking for help with a specific, different library, in all likelihood [`node-powershell` npm package](https://www.npmjs.com/package/node-powershell). Pointing out alternatives doesn't answer the question as asked and is therefore better suited as a [_comment_](https://stackoverflow.com/help/privileges/comment) (though you'll have to wait until you've earned 50 reputation points to be able to do so). – mklement0 Jun 21 '18 at 13:55
0

tl;dr:

For your PowerShell command to finish executing, you must call ps.dispose().


It looks like you're using the node-powershell npm package and have tried to adapt its sample code.

Unfortunately, as of this writing, this sample code is flawed, because it's missing a call to ps.dispose() code in the success case, which means that the PowerShell command never exits.

Here's a working example (assumes that the package was installed with npm install node-powershell):

const shell = require('node-powershell')

let ps = new shell({
  executionPolicy: 'Bypass',
  noProfile: true
});

// You can pack everything into the 1st argument or pass arguments as
// parameter-name-value objects; note the need to use null for [switch] parameters.
ps.addCommand("write-verbose", [ { verbose: null }, { message: 'hello, world'} ])

// IMPORTANT: ps.dispose() MUST be called for execution to finish.
ps.invoke()
  .then(output => {
    console.log(output)
    ps.dispose()  // This was missing from your code.
  })
  .catch(err => {
    console.log(err)
    ps.dispose()
  });
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Even facing same issue after adding ps,dispose() in success. – Sowmiya B Jun 22 '18 at 05:34
  • @SowmiyaB: The missing `ps.dispose()` was the only obvious problem with your code snippet. If you want further help, please updated your question to describe your environment (OS version, Node.js version, PowerShell version) and post an [MCVE (Minimal, Complete, and Verifiable Example)](http://stackoverflow.com/help/mcve). – mklement0 Jun 22 '18 at 12:18