错误:变量未定义“ping”而vbscript中的pingtest报告

时间:2012-12-01 10:40:42

标签: vbscript ping

我是Vbscript的新手,如果ping ip错误,我正在尝试获取pingtest报告! 这是我正在运行的脚本:我在

中收到错误
line 25:For Each ping In objPing  
      Select Case ping.StatusCode.......

我不知道该怎么做请帮帮我还是有其他替代解决方案?

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

1 个答案:

答案 0 :(得分:1)

您已在顶部设置Option Explicit,这意味着您必须先声明所有变量才能使用它们。

在致电该行之前添加Dim pingFor Each ping in objPing