经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP/ADO/VBScript » 查看文章
使用WMI得到计算机的信息
来源:jb51  时间:2019/4/30 10:12:24  对本文有异议

WMI是一项行业推荐规范,旨在为访问企业环境中的管理信息而开发一种标准化技术。该信息包括系统内存的状态、当前安装的客户应用程序清单,以及有关客户端状态的其他数据。
WMI是可伸缩的系统管理结构,该规范采用一个统一、基于标准且可扩展的面向对象接口。它提供与系统管理员信息和基础WMI API交互的标准方法,主要由系统管理应用程序开发人员和系统管理员用来访问和操作系统管理信息。
WMI可用来生成组织和管理系统信息的工具,使系统管理人员能够更密切的监视系统活动。
WMI提供了一套内置在Microsoft Windows操作系统中的丰富的系统管理服务,现在有大量的应用程序、服务和设备用其为信息技术操作和产品支持组织提供全方位的管理功能。基于WMI的管理系统的使用带来了更可靠的计算环境和更高的系统可靠性,从而节省了企业的开销。
WMI提供的大量的规范为许多高端应用程序,例如Microsoft Exchange、Microsoft SQL Server和Microsoft Internet信息服务(IIS)等实现如下管理任务。
1. 监视应用程序的运行情况
2. 检测瓶颈或故障
3. 管理和配置应用程序
4. 查询应用程序数据(使用对象关系的遍历和查询)
5. 执行无缝的本地或远程管理操作

下面我们通过一个例子来说明WMI的强大功能。以vb6为列:
引用”Microsoft WMI Scripting V1.1 Library”
代码如下:

  1. Option Explicit
  2. Dim WithEvents Sink As SWbemSink
  3. Dim j As Integer
  4. '功能:利用wmi组件得到计算机的信息,每一个小功能分开写,便于大家查阅
  5. Private Sub cmdDone_Click()
  6. Dim oWMINameSpace As SWbemServices
  7. Dim oLogicalDiskSet As SWbemObjectSet
  8. Dim oLogicalDisk As SWbemObject
  9. Dim ObjSet As Variant
  10. Dim sDrive As String
  11. Dim sValue As String
  12. Dim dblSize As Double
  13. Dim Obj As Variant
  14. Dim lIndex As Long
  15. Set oWMINameSpace = GetObject("winmgmts:")
  16. '得到驱动器的信息
  17. On Error Resume Next
  18. Set ObjSet = oWMINameSpace.InstancesOf("Win32_DiskDrive")
  19. For Each Obj In ObjSet
  20. List5.AddItem Obj.Caption & " - " & BytesToMegabytes(Obj.Size) & " GB"
  21. Next
  22. '得到每一个驱动器的详细信息
  23. On Error GoTo ErrorHandler
  24. 'Set oWMINameSpace = GetObject("winmgmts:")
  25. Set oLogicalDiskSet = oWMINameSpace.InstancesOf("Win32_LogicalDisk")
  26. For Each oLogicalDisk In oLogicalDiskSet
  27. On Error Resume Next
  28. sDrive = oLogicalDisk.deviceid
  29. ListView1.ListItems.Add , , sDrive
  30. lIndex = ListView1.ListItems.Count
  31. sValue = oLogicalDisk.Description & ""
  32. ListView1.ListItems(lIndex).SubItems(1) = sValue
  33. sValue = oLogicalDisk.FileSystem & ""
  34. ListView1.ListItems(lIndex).SubItems(2) = sValue
  35. sValue = oLogicalDisk.VolumeName & ""
  36. ListView1.ListItems(lIndex).SubItems(3) = sValue
  37. sValue = oLogicalDisk.VolumeSerialNumber & ""
  38. ListView1.ListItems(lIndex).SubItems(4) = sValue
  39. sValue = oLogicalDisk.Size & ""
  40. If IsNumeric(sValue) Then
  41. dblSize = BytesToMegabytes(CDbl(sValue))
  42. sValue = CStr(dblSize) & " MB"
  43. End If
  44. ListView1.ListItems(lIndex).SubItems(5) = sValue
  45. Next
  46. CleanUp:
  47. Set oLogicalDisk = Nothing
  48. Set oLogicalDiskSet = Nothing
  49. Set oWMINameSpace = Nothing
  50. Exit Sub
  51. ErrorHandler:
  52. MsgBox "" & Err.Description
  53. GoTo CleanUp
  54. End Sub
  55. Private Sub Command1_Click()
  56. Unload Me
  57. End Sub
  58. Private Function BytesToMegabytes(Bytes As Double) As Double
  59. Dim dblAns As Double
  60. dblAns = (Bytes / 1024) / 1024
  61. BytesToMegabytes = Format(dblAns, "###,###,##0.00")
  62. End Function
  63. Private Sub Command2_Click()
  64. Dim oWMINameSpace As SWbemServices
  65. Dim SystemSet As Variant
  66. Dim System As Variant
  67. Dim ObjSet As Variant
  68. Dim Obj As Variant
  69. Set oWMINameSpace = GetObject("winmgmts:")
  70. '操作系统
  71. Set SystemSet = oWMINameSpace.InstancesOf("Win32_OperatingSystem")
  72. For Each System In SystemSet
  73. List1.AddItem System.Caption
  74. List1.AddItem System.Manufacturer
  75. List1.AddItem System.BuildType & “” ‘Win9x下好像取不出来
  76. List1.AddItem System.Version
  77. List1.AddItem System.SerialNumber
  78. Next
  79. 'cpu
  80. Set ObjSet = oWMINameSpace.InstancesOf("Win32_Processor")
  81. For Each Obj In ObjSet
  82. List2.AddItem Obj.Caption
  83. List2.AddItem Obj.currentclockspeed & " Mhz"
  84. Next
  85. End Sub
  86. Private Sub Command3_Click()
  87. Dim oWMINameSpace As SWbemServices
  88. Dim ObjSet As Variant
  89. Dim Obj As Variant
  90. Dim Adapter As Variant
  91. '内存
  92. Set oWMINameSpace = GetObject("winmgmts:")
  93. Set ObjSet = oWMINameSpace.InstancesOf("Win32_PhysicalMemory")
  94. Dim i As String
  95. For Each Obj In ObjSet
  96. List3.AddItem BytesToMegabytes(Obj.capacity) & " MB" & " Chip"
  97. Next
  98. '网卡
  99. Set Sink = New SWbemSink
  100. Set Adapter = GetObject("winmgmts:")
  101. Adapter.InstancesOfAsync Sink, "Win32_NetworkAdapter"
  102. End Sub
  103. Private Sub Form_Load()
  104. j = 0
  105. End Sub
  106. Private Sub Sink_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
  107. Dim Adapter As Variant
  108. ‘得到所有的适配器信息
  109. Set Adapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & j & "")
  110. List4.AddItem Adapter.Description
  111. If IsNull(Adapter.MACAddress) Then
  112. List4.AddItem "No MAC Address"
  113. List4.AddItem ""
  114. Else
  115. List4.AddItem "Mac: " & Adapter.MACAddress
  116. List4.AddItem ""
  117. End If
  118. j = j + 1
  119. End Sub

这篇文章就介绍到这,更多的内容大家可以参考这篇文章:http://technet.microsoft.com/en-us/library/ee198932.aspx

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号