加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

vbscript – WScript.Shell.Exec – 从stdout读取输出

发布时间:2020-12-16 01:33:47 所属栏目:安全 来源:网络整理
导读:我的VBScript没有显示我执行的任何命令的结果.我知道命令被执行但我想捕获结果. 我已经测试了很多方法,例如: Const WshFinished = 1Const WshFailed = 2strCommand = "ping.exe 127.0.0.1"Set WshShell = CreateObject("WScript.Shell")Set WshShellExec =
我的VBScript没有显示我执行的任何命令的结果.我知道命令被执行但我想捕获结果.

我已经测试了很多方法,例如:

Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

Select Case WshShellExec.Status
   Case WshFinished
       strOutput = WshShellExec.StdOut.ReadAll
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll
 End Select

WScript.StdOut.Write strOutput  'write results to the command line
WScript.Echo strOutput          'write results to default output

但它不打印任何结果.如何捕获StdOut和StdErr?

WScript.Shell.Exec()立即返回,即使它启动的进程没有.如果您尝试立即阅读Status或StdOut,那里将不会有任何内容.

MSDN documentation建议使用以下循环:

Do While oExec.Status = 0
     WScript.Sleep 100
Loop

这会每100毫秒检查一次状态,直到它发生变化基本上,您必须等到该过程完成,然后您才能读取输出.

通过对代码进行一些小的更改,它可以正常工作:

Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

Do While WshShellExec.Status = WshRunning
     WScript.Sleep 100
Loop

Select Case WshShellExec.Status
   Case WshFinished
       strOutput = WshShellExec.StdOut.ReadAll()
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll()
 End Select

WScript.StdOut.Write(strOutput)  'write results to the command line
WScript.Echo(strOutput)          'write results to default output

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读