【问题标题】:Got error object required when connecting WMI连接 WMI 时需要错误对象
【发布时间】:2016-04-14 02:26:35
【问题描述】:

我想创建一个 Excel 日志文件来记录主机名、IP、ping 的状态以及 WMI 连接的状态。当我尝试运行脚本时,出现“需要对象”错误,但是当我检查代码时,我找不到应该更改代码的位置。

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

【问题讨论】:

    标签: vbscript wmi


    【解决方案1】:
    Set Ping = WshShell.Run("ping -n 1 " & hostname, 0, True)
    

    WshShell 返回一个整数而不是一个对象。 Set 是对象的唯一用户。所以只需删除set这个词。

    Wmi 可以自己进行 ping。这是命令行版本,但您可以将其插入您用于 NetworkAdaptor 的 WMI 语句中。

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

    寻求与 vbscript 相同的帮助类型wmic path win32_pingstatus get /?

    【讨论】:

    • 嗨面条感谢您的建议 :-) 现在我明白为什么错误不断出现 :-) 感谢您的解释。非常感谢再次感谢
    • 嗨面我想问更多如何远程连接另一台电脑?因为我有错误告诉服务器机器不可用
    • 如果主机名正确则不可用。主机名前面没有反斜杠,所以 computername 不是 \\computername
    • 对不起面条问你更多我会尝试删除反斜杠和错误出来的对象需要所有机器为什么会这样
    • 然后我得到了这个错误 error null: 8004100E 我理解的是这个错误意味着无效的命名空间,但我检查了命名空间是正确的
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多