nurul
nurul

Reputation: 15

Got error object required when connecting WMI

I want to create an Excel log file to record the hostname, IP, status of ping and the status of a WMI connection. When I try to run the script I've got an error "object required", but when I check the code I couldn't find where I should change the code.

On Error Resume Next
Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = Fso.OpenTextFile("file.txt", 1)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2

objExcel.Cells(1, 1).Value = "Hostname"
objExcel.Cells(1, 2).Value = "IP"
objExcel.Cells(1, 3).Value = "Ping"
objExcel.Cells(1, 4).Value = "WMI"

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

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

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

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

  Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & hostname & "\root\cimv2")
  Set IPconfig = objWMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

  If Err.Number = 0 Then
    objExcel.Cells(intRow, 4).Value = "Pass"
    For Each IP In IPconfig
      If Not IsNull(IP.IPAddress) Then
        For i = LBound(IP.IPAddress) To UBound(IP.IPAddress)
          objExcel.Cells(intRow, 2).Value = IP.IPAddress(i)
        Next
      Else
        objExcel.Cells(intRow, 2).Value = "N/A"
      End If
    Next
  Else
    objExcel.Cells(intRow, 4).Value = Err.Description
  End If

  intRow = intRow + 1
Loop

objExcel.Range("A1:B1:C1:D1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

InputFile.Close
On Error Goto 0

Set objExcel = Nothing
Set Fso      = Nothing
Set InputFile = Nothing
Set objWMIService = Nothing
Set WshShell = Nothing
Set Ping = Nothing
Set IPconfig = Nothing

MsgBox "Done Analyse"
objExcel.Quit
Wscript.Quit

Upvotes: 0

Views: 686

Answers (1)

user6017774
user6017774

Reputation:

Set Ping = WshShell.Run("ping -n 1 " & hostname, 0, True)

WshShell returns an integer not an object. Set is ONLY user for objects. So just delete set word.

Wmi can do it's own pinging. Here's the command line version but you can plug it in to the WMI statement you are using for NetworkAdaptor.

wmic /append:"textfile.txt" path win32_pingstatus where "address='127.0.0.1' and responsetime > 100" get responsetime,timestamprecord

For help that is same as vbscript type wmic path win32_pingstatus get /?

Upvotes: 2

Related Questions