Search code examples
node.jswmi

how to execute remote operation to windows machine via nodejs


I finally joined the community

So for my first question in this community:

Generally I want to execute some remote operations to a remote windows machine in node.js (of course I have permissions, credential and so on to the remote machine).

Specifically, right now I'm trying to retrieve list of services from windows machine. I've tried using the wmi-client package in order to do so:

const WmiClient = require('wmi-client');
var wmi = new WmiClient({
    username: '*****',          //credentials - username 
    password: '*****',          //credentials - password
    host: '*********',          // remote windows machine
});

wmi.query(`Select * from Win32_Service`, function (err, result) {
    console.log(result);
});

but I keep receiving error: Exit code: 44125. Invalid Global Switch.

I'll mention that using wmi in powershell make no issues for me. but when I trying to use the same technology in nodejs its failed. what am I doing wrong? Any other suggestions?

just to mention, when I need to retrieve same info from linux machine I easiliy do it using 'simple-ssh' package, without any issues:

    const SSH = require('simple-ssh');
    
    var ssh = new SSH({
      host: '*******',         // remote linux machine
      user: '*******',         // credentials - username
      pass: '*******'          // credentials - password
    });

      ssh.exec(`systemctl list-units --full -all`, {
          out: function(stdout) {
            // stdout as expected
          }
      }).start()});

but things getting complicated when trying to do the same for windows remote machine.

any ideas? Thank you very much!


Solution

  • seems like the following is working for me:

    var exec = require('node-ssh-exec');
    
    var config = {
        host: '*******',
        username: '***',
        password: '***'
    },
    command = 'sc query';
    
    exec(config, command, function (error, response) {
    if (error) {
        throw error;
    }
    

    the response is as expected, but for some reason the error associated with it is not empty:

    { errno: -4077, code: "ECONNRESET", syscall: "read", level: "connection-socket", }