Solved

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

Posted on 2011-09-15
2
491 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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