tomas / wmic

Wrapper around the Windows WMIC interface for Node.js.
12 stars 15 forks source link

Does not work with fields containing spaces #9

Open RogerHardiman opened 4 years ago

RogerHardiman commented 4 years ago

I have a problem with the library parsing the results of the wmic command From the command line I can execute this

wmic useraccount get name,sid,fullname

and I get this back

wmic useraccount get name,fullname,sid
FullName        Name                SID
                Administrator       S-1-5-21-3391238925-54190052-3516795850-500
                DefaultAccount      S-1-5-21-3391238925-54190052-3516795850-503
                Guest               S-1-5-21-3391238925-54190052-3516795850-501
Kiosk           kioskUser0          S-1-5-21-3391238925-54190052-3516795850-1002
Roger Hardiman  roger               S-1-5-21-3391238925-54190052-3516795850-1001
                WDAGUtilityAccount  S-1-5-21-3391238925-54190052-3516795850-504

However when I try this with the wmic library I get this output

[
{"FullName":"","Name        Name":"Administ","SID":"S-1-5-21-3391238925-54190052-3516795850-500"},
{"FullName":"","Name        Name":"DefaultA","SID":"S-1-5-21-3391238925-54190052-3516795850-503"},
{"FullName":"","Name        Name":"Guest","SID":"S-1-5-21-3391238925-54190052-3516795850-501"},
{"FullName":"Kiosk","Name        Name":"k           kioskUse","SID":"S-1-5-21-3391238925-54190052-3516795850-1002"},
{"FullName":"Roger Hardiman","Name        Name":"r Hardiman  roger","SID":"S-1-5-21-3391238925-54190052-3516795850-1001"},
{"FullName":"","Name        Name":"WDAGUtil","SID":"S-1-5-21-3391238925-54190052-3516795850-504"}]

My Javascript is

var wmic = require('./index.js');

wmic.get_values('useraccount', 'name,fullname,sid', null, function(err, values) {
  console.dir(values) // An array of disks
  console.log(JSON.stringify(values))
})

I assume there is a bug when parsing the text returned from the wmic command

RogerHardiman commented 4 years ago

Found the bug key.startPoint = lines[0].indexOf(key.string);

When processing the 2nd Key (which is 'name') it finds 'name' at Position 4 in this string (and not at position 20) FullName_ _ _ _ _ _ _ _Name_ _ _ _ _ _ _ _ _ _SID NOTE spaces changed to underscores for clarity Would it be better to set the Start Point to the size of the previous key + 1 ?

RogerHardiman commented 4 years ago

This workaround fixes it, using a new variable startPoint

function buildDataArray(rawInput){
  var lines = rawInput.toString().trim().split('\n'),
      data = [],
      keys = [],
      linePattern = /(\S*?\s\s+)/g,
      match,
      startPoint=0;        // ADDED BY ROGER

  while ((match = linePattern.exec(lines[0])) !== null) {
    if (match.index === linePattern.lastIndex) {
        linePattern.lastIndex++;
    }

    var key = {};

    key.string = match[0].trim();
    key.startPoint = startPoint;//lines[0].indexOf(key.string); // CHANGED BY ROGER
    key.keyLength = match[0].length;

    keys.push(key);
    startPoint = key.startPoint + key.keyLength; // ADDED BY ROGER
  }
tomas commented 4 years ago

Looks good, thanks. Would you submit a PR?

RogerHardiman commented 4 years ago

Hi Yes, will fork and do a PR when I get some spare time.

Roger