Ping Script

Ping Script

At times, when programming with VBScript, a programmer will want to know if a list of computers are connected to a network and powered on. Often times, prior to a major change, or when trouble shooting user issues, an IT professional will want to know if a device is connected to the network and powered on.

Pinging a small or large group of workstations can be accomplished by writing a VBScript which accesses the Windows Script Host by making use of WScript.Shell and Shell.run. Once these VBScripts are developed, most programmers keep them in a safe place for later use.

The program below will describe the steps necessary to accomplish this task and give you a working example to learn from.

VBScript Code for Ping Script

Ping Multiple Computers
Ping Multiple Computers | Source

Computer File

Computer File
Computer File | Source

Create Text File

Create a text file using notepad containing the host names of the computers that will need to be pinged. A list can also be copied to a notepad text file. Name the file C:\Temp\computers.txt

Within the computers.txt file, the list should look something like this.

computer1

computer2

compurer3

compurer4

The list can be as long as needed, but the script will run longer depending on the number of computers being pinged.

Create Ping Script

Go to Start - All Programs - Accessories – Notepad

Save the file as C:\Temp\PingComputers.vbs

Note: Adding .vbs to any .txt file creates an executable VBScript file.

Copy Ping Script Code

Copy and Paste the following code into Notepad.

‘**************************************************************

Option Explicit

Dim Shell, strCommand, strLine, ReturnCode, objDictionary, objWriteFile, objReadFile

'Set objects.

Set Shell = wscript.createObject("wscript.shell")

Set objDictionary = CreateObject("Scripting.Dictionary")

'Define file to write ping report.

Set objWriteFile = objFso.OpenTextFile("C:\Temp\pingReport.txt", 2, True)

'Define file to read computer list.

Set objReadFile = objFSO.OpenTextFile("C:\Temp\computers.txt", 1, False)

'Define command line.

strCommand = "ping -n 1 -w 300 " & strLine

'Loop through computers in file.

Do Until objReadFile.AtEndOfStream

strLine = objReadFile.ReadLine

strLine = trim(strLine)

'Run command and get return code

ReturnCode = Shell.Run(strCommand, 0, True)

'0 = pingable, 1 = no response

If ReturnCode = 0 Then

ReturnCode = "is pingable"

Else

ReturnCode = "is not pingable"

End If

objWriteFile.writeline strLine & "," & ReturnCode

Loop

'Cleanup objects.

Set Shell = nothing

Set objDictionary = nothing

Set objWriteFile = nothing

Set objReadFile = nothing

WScript.Quit()

'**************************************************************

Notice the comments in the script above explaining what it is doing.

Note-Comments are preceded with a "'".

Save your VBScript file and exit Notepad

VBScript Messagebox

Ping Report
Ping Report | Source

Run Ping Script

Run the script by double clicking on C:\Temp\PingComputers.vbs.

Browse to C:\Temp\pingReport and open this file. You will see your list of computers along with a comma and whether or not the computer is pingable.

More by this Author


Comments 2 comments

siva 5 years ago

HI iam getting a error @ 13th line char: 1.Error is Variable is undefined : 'objFSO'. Please tell me this script i want it very very urgently........or you can mail me the script to vsiva25@gmail.com.


who10 profile image

who10 5 years ago from Midwest Author

Looks like you're using upper case "FSO" in "objFSO". Make sure your it's the same as what you see in the script. I believe I used "objFso".

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working