本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2009年05月17日 统计字数: 1937字 阅读时间: 4分钟阅读 本文链接: https://soulteary.com/2009/05/17/vb-wmi-01.html ----- # [VB]WMI 脚本入门:第一部分,获取进程,内存,服务信息 WMI是一个强大的工具,一直有一个想法,就是把MS的一些东西用VB重写一下,最后聚合在一起,做一个实用的小工具。 文末有微软WMI页面。 ```vb Private Sub cmdCommand1_Click() Dim strComputerName As String strComputerName = VBA.Environ("ComputerName") Set wbemServices = GetObject("winmgmts:\\" & strComputerName) Set wbemObjectSet = wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration") For Each wbemObject In wbemObjectSet MsgBox "Total Physical Memory (kb): " & wbemObject.TotalPhysicalMemory Next End Sub Private Sub cmdCommand2_Click() Dim strComputer As String strComputer = VBA.Environ("ComputerName") Set wbemServices = GetObject("winmgmts:\\" & strComputer) Set wbemObjectSet = wbemServices.InstancesOf("Win32_Service") For Each wbemObject In wbemObjectSet List1.AddItem "Display Name: " & wbemObject.DisplayName & vbCrLf & " State: " & wbemObject.State & vbCrLf & " Start Mode: " & wbemObject.StartMode Next End Sub Private Sub cmdCommand3_Click() Dim strComputer As String strComputer = VBA.Environ("ComputerName") Set wbemServices = GetObject("winmgmts:\\" & strComputer) Set wbemObjectSet = wbemServices.InstancesOf("Win32_NTLogEvent") For Each wbemObject In wbemObjectSet List1.AddItem "Log File: " & wbemObject.LogFile & vbCrLf & "Record Number: " & wbemObject.RecordNumber & vbCrLf & "Type: " & wbemObject.Type & vbCrLf & "Time Generated: " & wbemObject.TimeGenerated & vbCrLf & "Source: " & wbemObject.SourceName & vbCrLf & "Category: " & wbemObject.Category & vbCrLf & "Category String: " & wbemObject.CategoryString & vbCrLf & "Event: " & wbemObject.EventCode & vbCrLf & "User: " & wbemObject.User & vbCrLf & "Computer: " & wbemObject.ComputerName & vbCrLf & "Message: " & wbemObject.Message & vbCrLf Next End Sub Private Sub cmdCommand4_Click() Dim strComputer As String strComputer = VBA.Environ("ComputerName") strComputer = "." ' Dot (.) equals local computer in WMI Set wbemServices = GetObject("winmgmts:\\" & strComputer) Set wbemObjectSet = wbemServices.InstancesOf("Win32_Process") For Each wbemObject In wbemObjectSet List1.AddItem "Name: " & wbemObject.Name & vbCrLf & " Handle: " & wbemObject.Handle & vbCrLf & " Process ID: " & wbemObject.ProcessID Next End Sub ``` 微软WMI入门教程离线版下载:[download id="13"]