vb.netwmi:几个 WMI 的例子(初级) - 4-vb教程



ss=MsoNormal style=\"MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none\" align=left>读取日志记录: prefix = o ns = \"urn:schemas-microsoft-com:office:office\" />

Imports

Imports .Management



Module Module1

Public Sub prefix = st1 ns = \"urn:schemas-microsoft-com:office:smarttags\" />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

Imports .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

Imports .Management



Module Module1

Public Sub Main

Dim strComputer As String = \".\", i, Value 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

Value = objItem.HandleCount

Console.WriteLine(\"打开句柄数 :\" & Value)

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

-----------------------------------------------------------------------



locator = createobject(\"WbemScripting.SwbemLocator\")
server = locator.ConnectServer (\"\\\\192.168.0.1\\root\\cimv2\",\"username\",\"password\")
p_obj = server.execquery(\"select * form Win32_operatingsystem\")
for each p_objswbem in p_obj
p_objswbem.reboot ‘从新启动
next



WMI 有关概述请参考 MSDN 或




Tags:  vb6.0教程 vb教程 vb初级教程 vb.netwmi

延伸阅读

最新评论

发表评论