JScript: how to run external command and get output?

18,210

Solution 1

var strOutput = oExec.StdOut.ReadAll();

In Javascript it is a call to a function and MUST include the parenthesis

Solution 2

var oShell = WScript.CreateObject("WScript.Shell");
var ret = oShell.Run('cmd /c dir', 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);
WScript.Echo("ret " + ret);

That assigns the exit code of the command to the ret variable, not its standard output.

To read the command's standard output, you can use cmd /c to run the command and redirect its standard output to a file, then read the file.

You can also use the WshScriptExec object and read the StdOut property, but if you use that object you can't control the window state like you can with WshShell.Run (like above).

Here is a sample script:

function runCommand(command) {
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var wshShell = new ActiveXObject("WScript.Shell");
  do {
    var tempName = fso.BuildPath(fso.GetSpecialFolder(2), fso.GetTempName());
  } while ( fso.FileExists(tempName) );
  var cmdLine = fso.BuildPath(fso.GetSpecialFolder(1), "cmd.exe") + ' /C ' + command + ' > "' + tempName + '"';
  wshShell.Run(cmdLine, 0, true);
  var result = "";
  try {
    var ts = fso.OpenTextFile(tempName, 1, false);
    result = ts.ReadAll();
    ts.Close();
  }
  catch(err) {
  }
  if ( fso.FileExists(tempName) )
    fso.DeleteFile(tempName);
  return result;
}

var output = runCommand("dir");
WScript.Echo(output);
Share:
18,210
Putnik
Author by

Putnik

AWS Certified Solutions Architect-Associate AWS Certified SysOps Administrator-Associate 10+ years with Linux etc :) Feel free to contact me via LinkedIn https://www.linkedin.com/in/alexander-lutchko-11b556b7/

Updated on June 27, 2022

Comments

  • Putnik
    Putnik about 2 years

    I'm running my JScript file using cscript.exe. In the script I need to call an external console command and get the output.

    Tried:

    var oShell = WScript.CreateObject("WScript.Shell");
    var oExec = oShell.Exec('cmd /c dir');
    WScript.Echo("Status "+oExec.Status);
    WScript.Echo("ProcessID "+oExec.ProcessID);
    WScript.Echo("ExitCode "+oExec.ExitCode);
    

    and

    var oShell = WScript.CreateObject("WScript.Shell");
    var ret = oShell.Run('cmd /c dir', 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);
    WScript.Echo("ret " + ret);
    

    but no luck: the command runs (most likely) without errors but I have no output. Please note 'cmd /c dir' here is just example to make sure I get any output at all.

    So, how should I do it?

    Update: I tried to convert this https://stackoverflow.com/a/6073170/1013183 to JScript but no luck too:

    var oShell = WScript.CreateObject("WScript.Shell");
    var oExec = oShell.Exec('cmd /c dir');
    var strOutput = oExec.StdOut.ReadAll;
    WScript.Echo("StdOut "+strOutput);
    
    var strOutput = oExec.StdErr.ReadAll;
    WScript.Echo("StdErr "+strOutput);
    

    The error is Microsoft JScript runtime error: Object doesn't support this property or method at var strOutput = oExec.StdOut.ReadAll; line

  • Putnik
    Putnik about 10 years
    Window state doesn't matter for me. I've updated the question (tried stdout) but it is still broken. Could you review, please?
  • Bill_Stewart
    Bill_Stewart about 10 years
    @Putnik - revised answer contains an example.