Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running command line silently with VbScript and getting output?

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 

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?

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

like image 798
Onat Avatar asked Apr 16 '11 22:04

Onat


People also ask

Can you run VBScript in CMD?

I googled it & got to know that we can run VBScript from command line by executing below command: For Example my vbscript name is Converter. vbs & it's present in folder D:\VBS .

What is WScript echo in VBScript?

You can use WScript. Echo to see exactly what VBScript passes out; if the string you see in the result of WScript. Echo works in a command-prompt window, it will work as the first argument for the Run method.


1 Answers

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 
like image 129
Mark Cidade Avatar answered Sep 23 '22 07:09

Mark Cidade