JScript:如何运行外部命令并获取输出? [英] JScript: how to run external command and get output?
问题描述
我正在使用cscript.exe运行我的JScript文件。
在脚本中,我需要调用外部 console 命令并获取输出。
I'm running my JScript file using cscript.exe. In the script I need to call an external console command and get the output.
尝试过:
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);
和
var oShell = WScript.CreateObject("WScript.Shell");
var ret = oShell.Run('cmd /c dir', 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);
WScript.Echo("ret " + ret);
但没有运气:命令运行(很可能)没有错误,但是我没有输出。
请注意,这里的'cmd / c dir'仅是确保我完全得到任何输出的示例。
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.
那么,我应该怎么做?
更新:
我试图将其转换为 https://stackoverflow.com/a/6073170/1013183 到JScript,但也没有运气:
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);
错误为 Microsoft JScript运行时错误:对象不支持此属性或方法
位于 var strOutput = oExec.StdOut.ReadAll;
行
推荐答案
var strOutput = oExec.StdOut.ReadAll();
在Java语言中,它是对函数的调用,必须包含括号
In Javascript it is a call to a function and MUST include the parenthesis
这篇关于JScript:如何运行外部命令并获取输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!