Chris
Chris

Reputation: 568

Test HTTP response in vbscript

I need to test the response time from a web url to see if it's getting a slow response. I have this from a tutorial website (can't remember which) but it doesn't do exactly what I want:

On Error Resume Next 
Set XMLHttp = CreateObject("MSXML2.ServerXMLHTTP") 
xmlhttp.open "GET", "http://website.url.com" , 0 
xmlhttp.send "" 
If Err.Number < 0 OR Err.Number > 0 Then 
  Dim objShell
  Set objShell = WScript.CreateObject ("WScript.shell")
  MsgBox "TimeOut"
  Set objShell = Nothing
  WScript.Quit 
Else
  MsgBox "OK"
End If 

Set xmlhttp = Nothing

This script only tests if the site timesout or not. I would need more detailed information such as even if it doesn't time out, how long was the response time, etc.

Upvotes: 1

Views: 8873

Answers (1)

Chris
Chris

Reputation: 568

OK, I found the solution:

Dim strHost

' Put your server here
strHost = "localhost"

if Ping(strHost) = True then
    Wscript.Echo "Host " & strHost & " contacted"
Else
    Wscript.Echo "Host " & strHost & " could not be contacted"
end if

Function Ping(strHost)

    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
        Ping = False
            'WScript.Echo "Status code is " & objRetStatus.StatusCode
        else
            Ping = True
            'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
            Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
            'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
        end if
    next
End Function 

All credits go to this website and it's author: http://www.windowsitpro.com/article/vbscript/how-can-i-use-a-vbscript-script-to-ping-a-machine-

Upvotes: 4

Related Questions