使用VbScript静默运行命令行并获取输出? [英] Running command line silently with VbScript and getting output?

查看:2478
本文介绍了使用VbScript静默运行命令行并获取输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够通过命令行运行程序,我想用VbScript启动它。我也想得到命令行的输出,并将其分配给一个变量,我想让所有这一切,在没有cmd窗口弹出来静默。我分别管理了两件事,但不是在一起。这是我到目前为止。
从cmd运行命令并获取输出:

I want to be able to run a program through command line and I want to start it with VbScript. I also want to get the output of the command line and assign it to a variable and I want all this to be done silently without cmd windows popping up. I have managed two things separately but not together. Here's what I got so far. Run the command from cmd and get output:

Dim WshShell, oExec
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("C:\snmpget -c public -v 2c 10.1.1.2 .1.3.6.1.4.1.6798.3.1.1.1.5.1")
x = oExec.StdOut.ReadLine
Wscript.Echo x

工作和做我想要的,除了cmd弹出一个短暂的时刻。

The above script works and does what I want except that cmd pops up for a brief moment.

这里是一个脚本,它将静默运行,但不会抓取输出

Here's a script that will run silently but won't grab the output

Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("C:\snmpset -c public -v 2c -t 0 10.1.1.2 .1.3.6.1.4.1.6798.3.1.1.1.7.1 i 1", 0, true)

让这两个在一起工作?

让我给你一个背景,为什么我想这样做。我基本上每5-10分钟轮询一个单位,我将获得脚本电子邮件或抛出一个消息框,当某个条件发生,但我不想看到cmd行在我的电脑上一整天弹出。有什么建议么?
感谢

Let me give you a background on why I want do to this. I am basically polling a unit every 5-10 minutes and I am going to get the script to email or throw a message box when a certain condition occurs but I don't want to see cmd line popping up all day long on my computer. Any suggestions? Thanks

推荐答案

您可以将输出重定向到文件,然后阅读文件:

You can redirect output to a file and then read the file:

return = WshShell.Run("cmd /c C:\snmpset -c ... > c:\temp\output.txt", 0, true)

Set fso  = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("c:\temp\output.txt", 1)
text = file.ReadAll
file.Close

这篇关于使用VbScript静默运行命令行并获取输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆