Passing Data from Windows CMD to Microsoft Jscript

Ran into another one: I need to get a set of data about the server using Microsoft Jscript. Tripping over Windows 2008 Server quirks again. It’s just really inconvenient for me to work with it.

Reading MSDN docs is enough to land you in the hospital. Got everything working by brute-forcing the parameters. So here’s a quick note on how to run something in the command line and dump the result into a Jscript variable.

// Declare a variable that will hold the command for cmd.exe
var shellcommand = "dir c:"
// initialize the shell
var WshShell = WScript.CreateObject("WScript.Shell");
var oExec = WshShell.Exec("%comspec% /c "+shellcommand);
// variable with the results
var result = oExec.StdOut.ReadAll();</p>
<p>//Extra goodies:
//execution status - oExec.Status
WScript.Echo("Status "+oExec.Status);
// if the process takes a while to run, it's good to know its pid - oExec.ProcessID
WScript.Echo("ProcessID "+oExec.ProcessID);
// this is how you get the exit code - oExec.ExitCode
WScript.Echo("ExitCode "+oExec.ExitCode);
// this is how you print the results
WScript.Echo("Result: \""+result+"\"");

As far as I understand the result variable comes back as type string. Definitely not an array. But I couldn’t find exact info on MSDN, so everyone’s free to believe whatever they want.