【问题标题】:Ping an IP, pop-up messages and save results to txt file - VBSPing一个IP,弹出消息并将结果保存到txt文件 - VBS
【发布时间】:2016-05-09 00:45:23
【问题描述】:

我正在制作一个简单的脚本来 ping 地址并将结果保存在 txt 文件中。

目的仅仅是为了方便不知道如何操作的外行用户:

  1. 打开“运行”框;
  2. 打开cmd;
  3. ping 地址;
  4. 复制结果;
  5. 转至 txt 文件。

到目前为止,我已经制作了这个 .bat 文件,但我希望它在 vbs 上具有更多功能和更好的视觉效果。

.BAT 方法

@ECHO OFF
:LOOPSTART
time /T >> PingTest.txt
ping 1.1.1.1 -n 100 >> %userprofile%\Desktop\PingTest.txt
exit
GOTO LOOPSTART

.VBS 方法

Dim WshShell, i
Set WshShell = CreateObject("WScript.Shell")
    WshShell.Popup "This program will run a connectivity test using PING", 5, "PING TEST!"

Dim strHost, strFile

strHost = "1.1.1.1"
strFile = "ping_test.txt"

PingCall strHost, strFile

Sub PingCall (strHost, outputfile)
    Dim Output, Shell, strCommand, ReturnCode

    Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
    Set Shell = CreateObject("wscript.shell")
    strCommand = "ping -n 100 " & strHost
    While(True)
            WshShell.Popup "Please, wait while test is being executed...", 1, "Test Running"
        ReturnCode = Shell.Run(strCommand, 0, True)
    Wend
End Sub

我遇到的问题 - 使用 VBS 脚本 -:

  1. 将ping结果保存到.txt文件;
  2. 显示一条消息,指示测试正在运行。我想要么 显示仍有多少数据包要发送或有一个盒子 在未完成时打开(“请稍等。这将关闭一次 测试已完成...");

就是这样。

我是不是太复杂了?

【问题讨论】:

    标签: vbscript ping connectivity


    【解决方案1】:

    你可以把这行写成快捷方式

    cmd /c ( Ping 127.0.0.1 > testPing.txt ) & ( date /t >> testPing.txt )
    

    您需要使用返回的WshShell.exec

    WshScriptExec 对象由 WshShell 对象的 Exec 方法返回。 Exec 方法在脚本或程序完成执行后或在脚本或程序开始执行之前返回 WshScriptExec 对象。

    (来自帮助)

    它使您可以访问文本所在的 StdOut。在上面做一个ReadAll

    但是 VBScript 可以自己执行 ping 操作

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    
    Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='104.43.195.251'")
    For Each objItem in colItems
        msgbox "Status" & objItem.statuscode & " Time " & objItem.ResponseTime
    Next
    

    用做100次。

    Do while Counter < 100
        Counter = Counter + 1 
        ...Ping code here ...
    Loop
    

    有关可用属性的帮助,在命令提示符中

    wmic PATH win32_PingStatus get /?
    

    PingStatus 没有任何方法,但如果有的话

    wmic PATH win32_PingStatus call /?
    

    您需要将脚本放入 HTA(基本上是一个名为 script.hta 的网页)并使用计数器更新网页。你可以从这里窃取代码https://msdn.microsoft.com/en-us/library/ms976290.aspx

    【讨论】:

    • 你的回答太棒了。我能理解你的想法和代码。您还为我提供了相关资源。你给了我方向。谢谢你。这里唯一的问题是我。我缺乏知识会导致我用头撞墙太久。提供信息和指导是好的,但有时不需要它或需要一种更直接的方法:这样做,这样和这样,这样,或者解决方案本身,就像@Hackoo 所做的那样。再次感谢您。
    【解决方案2】:

    你可以玩这个代码:

    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 = "-n 10"
    MyCmd = "Title Ping to " & DblQuote(Webaddress) &_
    " & Color 9B & Mode con cols=75 lines=3 & Echo Please wait a while the ping to " & DblQuote(Webaddress) & " is in progress ... & Time /T > "& TmpLogFile &_
    " & Ping " & Param & " " & Webaddress & " >> "& TmpLogFile &_
    " & cmd /U /C Type " & TmpLogFile & " > " & LogFile & " & Del " & TmpLogFile & "" 
    ws.Popup "This program will run a connectivity test using PING",5,"PING TEST!",vbInformation
    Call Run(MyCmd,1,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 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 console
        If Console = 1 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
        Run = Result
    End Function
    '**********************************************************************************************
    Function DblQuote(Str)
        DblQuote = Chr(34) & Str & Chr(34)
    End Function
    '**********************************************************************************************
    

    【讨论】:

    • 你的代码很棒。这正是我所需要的。这和我做的一样,但加上我永远不会像你那样做得好。谢谢你。顺便说一句,我可以在“MyCmd”中的消息“Echoed”中添加新行吗(cmd /c Title & Color & Mode & Echo)。我尝试了带和不带双引号的 & vbNewLine & ,但没有奏效,因为它将 & 之后的内容识别为新方法/参数(?)。
    猜你喜欢
    • 1970-01-01
    • 2019-05-27
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多