Ranjit Kumar
Ranjit Kumar

Reputation: 781

Error: variable undefined "ping" while pingtest report in vbscript

I am new to Vbscript and I am trying to get a pingtest report if ping ip is error!! Here is the script i am running :i am getting a error in

line 25:For Each ping In objPing  
      Select Case ping.StatusCode.......

I dont know what to do please help me or is there any alternate solution ??

Option Explicit
Dim objPing,objFile,objFSO,objExcel,objSheet
Dim myOutFile,query,Row,Col,PingMachines(2),i
myOutFile = "c:\temp\log"
Col = 1
Row = 1


PingMachines(0)="192.168.1.1"
PingMachines(1)="192.168.1.2"

Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
objExcel.Columns(1).ColumnWidth = 16 
objExcel.Columns(2).ColumnWidth = 20 
objSheet.Cells(1,Col).Value = "Server" 'Contain IP address
objSheet.Cells(1,Col+1).Value = "Date" ''Date Time 
objSheet.Cells(1,Col+2).Value = "Response" 'Response of the ping
Row=Row+1
For i=LBound(PingMachines) To UBound(PingMachines) -1
    query="select * from Win32_Pingstatus where address ='127.0.0.1'"
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery(query)
    For Each ping In objPing  
      Select Case ping.StatusCode
        Case 0
          response="Reply from " & ping.ProtocolAddress 
        Case 11002     
          response="Destination Net Unreachable" 
        Case 11003
          response="Destination Net Unreachable"
        Case 11010
          response="Request Timed Out"     
      End Select
      objSheet.Cells(Row,1).Value=PingMachines(n)
      objSheet.Cells(Row,2).Value=Now
      objSheet.Cells(Row,3).Value=response
      Row=Row+1
    Next
Next
objExcel.ActiveWorkbook.SaveAs("c:\temp\test.xls") 
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

enter image description here

Upvotes: 0

Views: 558

Answers (1)

Jap Mul
Jap Mul

Reputation: 18759

You have set Option Explicit at the top which means you have to declare all variables before you can use them.

Add a Dim ping before you call the line: For Each ping in objPing

Upvotes: 1

Related Questions