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

查看:29
本文介绍了使用 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)

有没有办法让这两者协同工作?

Is there a way to get these two to work together?

让我向您介绍一下我为什么要这样做的背景.我基本上每 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:	empoutput.txt", 0, true)

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

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

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