January 19, 2010

Script to ping a list of machines and export result in Excel File

Please create a file "MachineList.txt" and include machine name in this file.

copy the below script in notepad and save as "ping_result.vbs" file.
run the script in command prompt by specifying the same path.

run method: cscript ping_result.vbs


Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
intRow = 2

objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "Results"

Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("MachineList.Txt")

Do While Not (InputFile.atEndOfStream)
HostName = InputFile.ReadLine

Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)

objExcel.Cells(intRow, 1).Value = HostName

Select Case Ping
Case 0 objExcel.Cells(intRow, 2).Value = "On Line"
Case 1 objExcel.Cells(intRow, 2).Value = "Off Line"
End Select

intRow = intRow + 1

objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True

1 comment:

  1. Hi! excellent solution !! I am trying to figure how to capture The computer name and add it as result as well