用VbScript静静地运行命令行并获取输出?

我希望能够通过命令行运行程序,我想用VbScript启动它。 我也想得到命令行的输出,并将其分配给一个variables,我希望所有这一切都静静地完成,没有CMD窗口popup。 我已经分别pipe理了两件事情,但不是在一起。 这是我到目前为止。 从cmd运行命令并获取输出:

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 

上面的脚本工作,做我想要的,只是CMDpopup一个短暂的时刻。

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

 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行。 有什么build议么? 谢谢

您可以将输出redirect到一个文件,然后读取该文件:

 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 

我已经采取了这个和其他各种意见,并创造了一个更先进的function,运行一个应用程序,并获得输出。

调用函数示例:仅输出目录C:\的DIR列表。 输出将返回到variablesCommandResults,并保留在C:\ OUTPUT.TXT中。

 CommandResults = vFn_Sys_Run_CommandOutput("CMD.EXE /C DIR C:\ /AD",1,1,"C:\OUTPUT.TXT",0,1) 

function

 Function vFn_Sys_Run_CommandOutput (Command, Wait, Show, OutToFile, DeleteOutput, NoQuotes) 'Run Command similar to the command prompt, for Wait use 1 or 0. Output returned and 'stored in a file. 'Command = The command line instruction you wish to run. 'Wait = 1/0; 1 will wait for the command to finish before continuing. 'Show = 1/0; 1 will show for the command window. 'OutToFile = The file you wish to have the output recorded to. 'DeleteOutput = 1/0; 1 deletes the output file. Output is still returned to variable. 'NoQuotes = 1/0; 1 will skip wrapping the command with quotes, some commands wont work ' if you wrap them in quotes. '---------------------------------------------------------------------------------------- On Error Resume Next 'On Error Goto 0 Set f_objShell = CreateObject("Wscript.Shell") Set f_objFso = CreateObject("Scripting.FileSystemObject") Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VARIABLES If OutToFile = "" Then OutToFile = "TEMP.TXT" tCommand = Command If Left(Command,1)<>"""" And NoQuotes <> 1 Then tCommand = """" & Command & """" tOutToFile = OutToFile If Left(OutToFile,1)<>"""" Then tOutToFile = """" & OutToFile & """" If Wait = 1 Then tWait = True If Wait <> 1 Then tWait = False If Show = 1 Then tShow = 1 If Show <> 1 Then tShow = 0 'RUN PROGRAM f_objShell.Run tCommand & ">" & tOutToFile, tShow, tWait 'READ OUTPUT FOR RETURN Set f_objFile = f_objFso.OpenTextFile(OutToFile, 1) tMyOutput = f_objFile.ReadAll f_objFile.Close Set f_objFile = Nothing 'DELETE FILE AND FINISH FUNCTION If DeleteOutput = 1 Then Set f_objFile = f_objFso.GetFile(OutToFile) f_objFile.Delete Set f_objFile = Nothing End If vFn_Sys_Run_CommandOutput = tMyOutput If Err.Number <> 0 Then vFn_Sys_Run_CommandOutput = "<0>" Err.Clear On Error Goto 0 Set f_objFile = Nothing Set f_objShell = Nothing End Function 

否则,请查找将输出分配给剪贴板(在第一个脚本中),然后在第二个脚本中分析剪贴板值。

如果你有其他的方法,请给我build议。

@Mark Cidade

谢谢Mark! 这解决了几天的研究,想知道如何从PHP WshShell调用这个。 所以感谢你的代码,我想…

 function __exec($tmppath, $cmd) { $WshShell = new COM("WScript.Shell"); $tmpf = rand(1000, 9999).".tmp"; // Temp file $tmpfp = $tmppath.'/'.$tmpf; // Full path to tmp file $oExec = $WshShell->Run("cmd /c $cmd -c ... > ".$tmpfp, 0, true); // return $oExec == 0 ? true : false; // Return True False after exec return $tmpf; } 

这对我来说就是我的工作。 随意使用和修改根据您的需求。 您可以随时在函数中添加function来自动读取tmp文件,将其分配给variables和/或将其返回,然后删除tmp文件。 再次感谢@Mark!

 Dim path As String = GetFolderPath(SpecialFolder.ApplicationData) Dim filepath As String = path + "\" + "your.bat" ' Create the file if it does not exist. If File.Exists(filepath) = False Then File.Create(filepath) Else End If Dim attributes As FileAttributes attributes = File.GetAttributes(filepath) If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then ' Remove from Readonly the file. attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly) File.SetAttributes(filepath, attributes) Console.WriteLine("The {0} file is no longer RO.", filepath) Else End If If (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then ' Show the file. attributes = RemoveAttribute(attributes, FileAttributes.Hidden) File.SetAttributes(filepath, attributes) Console.WriteLine("The {0} file is no longer Hidden.", filepath) Else End If Dim sr As New StreamReader(filepath) Dim input As String = sr.ReadToEnd() sr.Close() Dim output As String = "@echo off" Dim output1 As String = vbNewLine + "your 1st cmd code" Dim output2 As String = vbNewLine + "your 2nd cmd code " Dim output3 As String = vbNewLine + "exit" Dim sw As New StreamWriter(filepath) sw.Write(output) sw.Write(output1) sw.Write(output2) sw.Write(output3) sw.Close() If (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then Else ' Hide the file. File.SetAttributes(filepath, File.GetAttributes(filepath) Or FileAttributes.Hidden) Console.WriteLine("The {0} file is now hidden.", filepath) End If Dim procInfo As New ProcessStartInfo(path + "\" + "your.bat") procInfo.WindowStyle = ProcessWindowStyle.Minimized procInfo.WindowStyle = ProcessWindowStyle.Hidden procInfo.CreateNoWindow = True procInfo.FileName = path + "\" + "your.bat" procInfo.Verb = "runas" Process.Start(procInfo) 

它会将你的.bat文件保存到“当前用户的Appdata”中,如果它不存在,删除属性,然后在编写你的cmd代码后将其设置为“hidden”属性,并静默运行,并捕获所有的输出保存到文件,所以如果你想保存所有输出的cmd文件只是添加你这样的

 code > C:\Users\Lenovo\Desktop\output.txt 

只需用你的.bat文件代码或命令replace单词“代码”,然后输出文件的目录,我最近search了很多,如果你想在VB或C#运行.bat文件,或者只是简单地添加我写的