Solved

VBScript Ping opens window for each test - how to stop?

Posted on 2011-09-15
2
489 Views
Last Modified: 2012-06-21
Experts,

I've borrowed the great code here:

http://customerfx.com/pages/crmdeveloper/2008/05/19/ping-a-remote-server-using-vbscript.aspx

*Credits to Mr. Ryan Farley





If possible though, I'd like to change the default behaviour of this script.  Each server that gets tested opens a quick command window for the test and just as fast as the test is done - it goes away.

Is there an option to run a script such as this, where there isn't any console/cmd window displayed?
Function Ping(Target)
Dim results

    On Error Resume Next

    Set shell = CreateObject("WScript.Shell")
    
    ' Send 1 echo request, waiting 2 seconds for result 
    Set exec = shell.Exec("ping -n 1 -w 2000 " & Target)
    results = LCase(exec.StdOut.ReadAll)
    
    Ping = (InStr(results, "reply from") > 0)
End Function


If Ping("192.168.1.100") Then
    ' Do something to access the resource
End If

Open in new window

0
Comment
Question by:usslindstrom
2 Comments
 
LVL 14

Accepted Solution

by:
brendanmeyer earned 500 total points
ID: 36547257
i would use the script from
http://larsmichelsen.com/vbs/quickie-how-to-ping-a-host-in-vbs-i-got-two-ways/

 
Function Ping(strHost)
    Dim oPing, oRetStatus, bReturn
    Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & strHost & "'")
 
    For Each oRetStatus In oPing
        If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
            bReturn = False
 
            ' WScript.Echo "Status code is " & oRetStatus.StatusCode
        Else
            bReturn = True
 
            ' Wscript.Echo "Bytes = " & vbTab & oRetStatus.BufferSize
            ' Wscript.Echo "Time (ms) = " & vbTab & oRetStatus.ResponseTime
            ' Wscript.Echo "TTL (s) = " & vbTab & oRetStatus.ResponseTimeToLive
        End If
        Set oRetStatus = Nothing
    Next
    Set oPing = Nothing
 
    Ping = bReturn
End Function



If Ping("localhos3t") Then
	MsgBox("OK: Host localhost is reachable.")
Else
	MsgBox("ERROR: Can not reach host localhost.")
End If

Open in new window

0
 
LVL 5

Author Closing Comment

by:usslindstrom
ID: 36547278
That was exactly what I needed.  Thanks!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSIS 2010 Convert to SSIS 2013 On Different Box 3 58
Copy exact format 27 105
Command to modify Registry entry 5 108
Batch/VBScript : Disable Windows tasks 7 34
Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question