几个 WMI 的例子(初级) - 4

读取日志记录:

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

微信扫码订阅
UP更新不错过~
关注
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

xDotNet

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值