2017-02-22 4 views
-1

Powershell aus NodeJS zu nennen habe ich diese Powershell, die ich in der Regel wie folgt aufrufen:Nicht in der Lage

powershell -ExecutionPolicy Bypass -File "D:\tmp\getmember2.ps1" -groupnames "ABC" 

Jetzt muss ich aus meiner NodeJS nennen. Also, das ist, was ich schaffen:

var spawn = Meteor.npmRequire("child_process").spawn; 
    child = spawn("powershell.exe",["-ExecutionPolicy ByPass -File d:\\tmp\\getmember2.ps1 -groupnames \"ABC\""]); 
    child.stdout.on("data",function(data){ 
     console.log("Powershell Data: " + data); 
    }); 
    child.stderr.on("data",function(data){ 
     console.log("Powershell Errors: " + data); 
    }); 
    child.on("exit",function(){ 
     console.log("Powershell Script finished"); 
    }); 

    child.stdin.end(); 

Jedoch habe ich diesen Fehler bin immer:

I20170222-16:58:25.257(8)? API started 
    I20170222-16:58:26.175(8)? Powershell Errors: At line:1 char:2 
    I20170222-16:58:26.174(8)? 
    I20170222-16:58:26.175(8)?  + CategoryInfo   : ParserError: (-:String) 
    [], ParentContainsErrorR 
    I20170222-16:58:26.175(8)? + - <<<< ExecutionPolicy ByPass -File d:\tmp\getmember2.ps1 
    I20170222-16:58:26.176(8)? Powershell Errors: ecordException 
    I20170222-16:58:26.176(8)? Powershell Errors:  + FullyQualifiedErrorId : MissingExpressionAfterOperator 
    I20170222-16:58:26.176(8)? Powershell Errors: 
    I20170222-16:58:26.177(8)? 
    I20170222-16:58:26.174(8)? Powershell Errors: Missing expression after unary operator '-'. 
    I20170222-16:58:26.259(8)? Powershell Script finished 

Jede Idee, warum es nicht funktioniert?

Antwort

0

Ändern Sie meinen Code, um child_process.exec() anstelle von spawn() zu verwenden. in diesem beschrieben als link:

Spawn wird am besten verwendet, wenn Sie das Kind Prozess wollen eine große Menge von Daten zu Knoten zurückzukehren - Bildverarbeitung, binäre Daten usw. Verwenden exec Lesen Programme auszuführen, die Ergebnis zurück Status statt Daten.

Code geändert auf unten und führen Sie meine Powershell sauber.

var child = exec('Powershell.exe -executionpolicy ByPass -File d:\\tmp\\getmember.ps1 -processdir d:\\temp -groupnames "ABC"', 
    function(err, stdout, stderr){ 
     //callback(err) 
     if(err){ 
     console.error(err); 
     }else { 
     console.log("should be no issue"); 
     } 
    }); 

    child.stdout.on('data', function(data) { 
     // Print out what being printed in the powershell 
     console.log(data); 
    }); 

    child.stderr.on('data', function(data) { 
     //print error being printed in powershell 
     console.log('stderr: ' + data); 

    }); 

    child.on('close',function(code){ 
     // To check the result of powershell exit code 
     console.log("exit code is "+code); 
     if(code==0) 
     { 
     console.log("Powershell run successfully, exit code = 0 ") 
     }else { 
     console.log("ERROR, powershell exited with exit code = "+ code); 
     } 
    }); 

    child.stdin.end();