5

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?

3
  • turn it into a batch file / ps script and run it without all those fragile arguments. Commented Mar 29, 2016 at 1:59
  • what fragile arguments? Commented Mar 29, 2016 at 4:46
  • the command-line arguments you try to pass... note the answer in the link uses a .ps file... Commented Mar 29, 2016 at 4:47

4 Answers 4

5

Another way...

exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
  // do whatever with stdout
})
Sign up to request clarification or add additional context in comments.

1 Comment

This one worked great for me, has a nice neat lay out to
3

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.

Comments

2

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);
});

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.