VBSCRIPT:TRACERT和PING WEB ADDRESS并写入文本文件而不显示命令提示符弹出窗口

时间:2014-05-08 13:26:00

标签: shell vbscript ping wsh traceroute

我需要在VBS中编写一个诊断实用程序,我将在Windows应用程序安装程序中打包。

我希望该实用程序在用户安装应用程序时以静默方式运行。

我试过了:

Set pingCXS = objShell.Run("tracert -h 9 webaddress", 0, True)

  Set pingCXSOutput = pingCXS.StdOut

  strpingCXSOutput = pingCXSOutput.ReadAll

但它只返回错误代码而不是整个ping信息。

当我使用run方法时,会弹出一个命令窗口:

在没有Windows弹出窗口的情况下traceRT webaddress的任何其他方法?

同样使用批处理文件对我来说不是一个好选择,因为我必须在实用程序中使用一些WMI查询,这将需要批处理文件中的管理员权限...

请帮忙

1 个答案:

答案 0 :(得分:0)

试试这段代码:

Option Explicit
Dim ws,fso,TmpLogFile,Logfile,MyCmd,Webaddress,Param
Set ws = CreateObject("wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject") 
TmpLogFile = "TmpFile.txt"
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "log"
If fso.FileExists(LogFile) Then fso.DeleteFile LogFile
webaddress = "www.stackoverflow.com"
Param = "-h 9"
MyCmd = "Tracert " & Param & " " & Webaddress & " >> "& TmpLogFile &_
" & cmd /U /C Type " & TmpLogFile & " > " & LogFile & " & Del " & TmpLogFile & "" 
Call Run(MyCmd,0,True)
ws.run LogFile
'**********************************************************************************************
Function Run(StrCmd,Console,bWaitOnReturn)
    Dim ws,MyCmd,Result
    Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the MS-DOS console
    If Console = 0 Then
        MyCmd = "CMD /C " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            MsgBox "Success"
        Else
            MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
'A value of 1 to show the MS-DOS console
    If Console = 1 Then
        MyCmd = "CMD /K " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            MsgBox "Success"
        Else
            MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
    Run = Result
End Function