读取日志记录:
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim strComputer = "magicdog"
Dim wbemServices, wbemObjectSet, wbemObject
wbemServices = GetObject("winmgmts:\\" & strComputer)
wbemObjectSet = wbemServices.InstancesOf("Win32_NTLogEvent")
For Each wbemObject In wbemObjectSet
Console.WriteLine("日志文件:" & wbemObject.LogFile)
Console.WriteLine("记录号:" & wbemObject.RecordNumber)
Console.WriteLine("类型:" & wbemObject.Type)
Console.WriteLine("产生时间:" & wbemObject.TimeGenerated)
Console.WriteLine("源名称:" & wbemObject.SourceName)
Console.WriteLine("事件代码:" & wbemObject.EventCode)
Console.WriteLine("用户" & wbemObject.User)
Console.WriteLine("计算机名:" & wbemObject.ComputerName)
Console.WriteLine("信息:" & wbemObject.Message)
Next
End Sub
End Module
---------------------------------------------------------------------------------------
删除日志文件:(请先备份)
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim strComputer = "."
Dim objWMIService, objLogFiles, objLogFile, logs, mylogs(3)
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Backup)}!\\" & strComputer & "\root\cimv2")
mylogs(1) = "application"
mylogs(2) = "system"
mylogs(3) = "security"
For Each logs In mylogs
objLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile where LogFileName='" & logs & "'")
For Each objLogFile In objLogFiles
objLogFile.ClearEventLog()
Next
Next
End Sub
End Module
---------------------------------------------------------------------------------------
使用性能计数器:
Imports System
Imports System.Management
Module Module1
Public Sub Main()
Dim strComputer As String = ".", i, intValue As Integer
Dim colItems, objWMIService, objItem
objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
For i = 1 To 5
Threading.Thread.Sleep(1000)
colItems = objWMIService.ExecQuery("Select * From Win32_PerfRawData_PerfProc_Process Where Name = 'devenv'")
For Each objItem In colItems
intValue = objItem.HandleCount
Console.WriteLine("打开的句柄数 :" & intValue)
Next
Next
End Sub
End Module
使用Getobject函数创建对象多用于脚本中,所以参数为WMI脚本库的标记名"winmgmts:"和目标电脑名,上述的strComputer="."相当于localhost,若在远程计算机上执行则需将”.”改这相应的计算机名。
其中:
ManagementObject:单个管理对象
ManagementClass:单个管理类
ManagementObjectSearcher:查询管理对象及管理类的集合
ManagementEventWatcher:监视来自 WMI 的事件
ManagementQuery:用作所有查询类的基础。
也可以从脚本中创建:
Set oLocator = CreateObject("WbemScripting.SWbemLocator") ' New SWbemLocator
If Err.Number <> 0 Then
MsgBox "Windows Management (WMI) 不能被创建” & Err.Description
WScript.Quit(0)
End If
-----------------------------------------------------------------------
set locator = createobject("WbemScripting.SwbemLocator")
set server = locator.ConnectServer ("\\192.168.0.1\root\cimv2","username","password")
set p_obj = server.execquery("select * form Win32_operatingsystem")
for each p_objswbem in p_obj
p_objswbem.reboot ‘从新启动
next
WMI 的有关概述请参考 MSDN 或 http://www.csdn.net/develop/Article/19/19157.shtm