machinebit
machinebit

Reputation: 365

How to execute windows powershell command using childprocess and nodejs?

I am trying to run a powershell command through a nodejs script. I have found the following two articles which have shown me something similar to what I am trying to acheive: Execute Windows Commands with Nodejs Execute powershell script from nodejs

On a button click event, I am trying to list the usb devices currently attached to the system along with its Drive Letter (C, D, E etc). If I run the command in the powershell on its own, it works (I am unable to get it to display the drive letter though). However, if I run it as part of my script it does not work. Below is my code:

if (process.platform === 'win32' || process.platform === 'win64') {
    exec("powershell.exe",["GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType –eq 'USB' }"], function (err, stdout, stderr) {
        console.log(err);
        console.log(stdout);
        console.log(stderr);
    });
}

What am I doing wrong?

Upvotes: 5

Views: 22430

Answers (4)

Morsmalleo
Morsmalleo

Reputation: 95

You'll want to request child_process with..

var exec = require("child_process").exec;

Then you'll want to call exec() to execute a child process, followed by the commands you want the child process to execute, you'll need to do this with a callback function as well as seen in the snippet below, you need this to catch errors in case something goes wrong and you need to fix it.

exec('CommandHere', {'shell':'powershell.exe'}, (error, stderr, stdout) => {
    if (error !== null) {
        // Do something
    }
});

Here's an example using Powershell's set-location and gci commands to search recursively for a file within a specified directory and return it's relative path for Windows...

var exec = require("child_process").exec;
var folder = "C:\\Users\\winUser\\just\\some\\folder\\location";
var file = "test.txt";

exec('set-location ' + '"' + folder + '"' + 
';' + ' gci -path ' + '"' + folder + '"' + 
' -recurse -filter ' + '"' + file + '"' + 
' -file | resolve-path relative', 
{'shell':'powershell.exe'}, (error, stderr, stdout) => {
    var filePath = stdout.substring(stdout.indexOf(".\\") + 2).trim("\n");
    if (error !== null) {
        console.log("Cannot locate the given file \n");
        console.log(error);
    }

    console.log("File located! \n Path: " + filePath);

});

Hope this helps anyone facing this issue.

Upvotes: 1

oldboy
oldboy

Reputation: 5954

Another way...

exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
  // do whatever with stdout
})

Upvotes: 5

Ran Cohen
Ran Cohen

Reputation: 61

You can use 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.

Upvotes: 3

n01d
n01d

Reputation: 1077

I believe you shold pass the code with -command before it. Default PowerShell syntax is: powershell.exe -command "get-wmiobject ...".

Something like this:

exec("powershell.exe",["-command \"Get-WmiObject -Class win32_diskdrive | Where { $_.InterfaceType -eq 'USB' }\""], function (err, stdout, stderr) {
    console.log(err);
    console.log(stdout);
    console.log(stderr);
});

Upvotes: 2

Related Questions