最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to execute windows powershell command using childprocess and nodejs? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Mar 29, 2016 at 1:55 machinebitmachinebit 3653 gold badges7 silver badges17 bronze badges 3
  • turn it into a batch file / ps script and run it without all those fragile arguments. – dandavis Commented Mar 29, 2016 at 1:59
  • what fragile arguments? – machinebit 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... – dandavis Commented Mar 29, 2016 at 4:47
Add a comment  | 

4 Answers 4

Reset to default 5

Another way...

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

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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论