• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-01 21:48 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网

读取日志记录:

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 或


文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网