转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
Managing Windows with WMI         ★★★★

Managing Windows with WMI

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2830 更新时间:2009/4/23 16:39:38
vents are added to a log.
  • Registry Provider. Allows Registry keys to be created, read, and written. WMI events can be generated when specified Registry keys are modified.
  • Performance Counter Provider. Exposes the raw performance counter information used to compute the performance values shown in the System Monitor tool. Any performance counters installed on a system will automatically be visible through this provider. Only supported on Windows 2000.
  • Active Directory® Provider. Acts as a gateway to all information stored in the Microsoft Active Directory services. Allows information from both WMI and Active Directory to be accessed using a single API.
  • Windows Installer Provider. Allows complete control of the Windows Installer and installation of software through WMI. Also supplies information about any application installed with the Windows Installer.
  • SNMP Provider. Acts as a gateway to systems and devices that use the Simple Network Management Protocol (SNMP) for management. SNMP MIB object variables can be read and written. SNMP traps can be automatically mapped to WMI events.
  • View Provider. Allows new aggregated classes to be built up from existing classes. Source classes can be filtered for only the information of interest, information from multiple classes can be combined into a single class, and data from multiple machines can be aggregated into a single view.
  • See the WMI system schema chart at http://www.msdn.microsoft.com/voices/news/wmichart.asp , which shows some of the more important WMI system and device classes that are available in Windows 2000 and the properties, methods, and associations they define. The entire schema for the providers alone just listed is just too large and rich in information to fully publish here, but this chart will be a useful reference tool as you explore what WMI has to offer and can serve as a starting point to learn more about what is available. WMI providers are not just limited to core operating system functionality, however. Numerous providers have been developed both by Microsoft and third parties for other key applications and services that run on Windows platforms. See the sidebar called "How others are making use of WMI" at http://www.msdn.microsoft.com/voices/news/wmisidebar1.asp .

    Now that we''''ve defined what WMI is and why you will find it useful in managing your systems, we''''ll take a look at how you can do that with its scripting support. As is usual with scripting on Windows platforms, you have a range of script language choices available to you through the Windows Scripting Host (WSH). These include Microsoft Visual Basic® Scripting Edition (VBScript), Microsoft JScript®, and any script language that supports Microsoft ActiveScripting technology, such as Perl. WMI classes can be just as easily accessed via C++, Visual Basic, Active Server Pages (ASP), and scripts within standard HTML pages.

    There are so many things that can be done with WMI instrumentation; there is no way to give a representative example of every major subsystem in this article. Fortunately, once you understand how to access a class, read or write a property, execute a method, or receive an event in one class, it is exactly the same for every other WMI class. To illustrate some of the capabilities of WMI, I have selected several examples, written in VBScript. These examples include:

    • Enumerate a list of all installed services and then identify only those services that are stopped but are set to "Automatic" start mode.
    • List all the logical disk partitions on my computer and determine if any have less that 20 percent free space left.
    • Change the time delay before the default selected operating system is booted.
    • Execute a backup on my "Application" event log and then clear the log file.
    • Reboot my coworker''''s desktop system. (Don''''t worry, he had to give me security permission to do this and it gives me a chance to talk about security.)
    • Launch the Notepad utility through a WMI method.
    • Set up an event consumer to watch for any new events that are added to the event log.
    • Reconfigure my event consumer script to request an event whenever my CPU utilization exceeds 50 percent.

    List All Services on the System

    In this example we use the Win32_Service class to output a list of all the services that are installed on the system, regardless of whether they are running:

    Set ServiceSet =
    GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_Service")
    for each Service in ServiceSet
    WScript.Echo Service.Description
    Next

    Let''''s briefly walk through this example and understand what is happening at each stage. First, we request an object in the GetObject() call from the WMI service (WinMgmt). We do this by submitting a request for all instances of the Win32_Service class through the InstancesOf() method. The result of this call is a list of instances of the Win32_Service class.

    After getting the instances back in the ServiceSet variable, all that is left to do is iterate through them and display the property of interest, the Description field, which contains the friendly name of the service. It is important to note here that the Description property is being handled as an automation property of the Service object, allowing for much more intuitive scripting. Alternately, the exact same results can be obtained by using an SQL query and submitting it to the ExecQuery() method:

    Set ServiceSet =
    GetObject("winmgmts:{impersonationLevel=impersonate}")
    .ExecQuery("select * from Win32_Service")

    The SQL query asks for all instances (and all properties of those instances) to be selected and returned. The resulting set of objects that are returned are exactly equal using this form, but as will be clear in the examples to follow, the query form can be used to refine the number of properties, instances, or both returned to only those of interest. Most importantly, either mechanism can be used for any class supplied through WMI that supports being enumerated.

    One final note about this example and those that follow involves the security model being used. You may have seen the statement "{impersonationLevel=impersonate}" as part of the GetObject() call just shown. In essence, this tells the system to use your current login credentials as those to be presented when asking for data or executing methods. It is the same as though you were calling the native APIs directly. This applies for any requests made either on your local machine or a remote machine. In any case, the user the system sees is the one defined by your current login. It is possible, of course, to specify different credentials for requests if desired, but this is outside the scope of this article. Also, on Windows 2000 the "{impersonationLevel=impersonate}" statement can be omitted from the script, as "impersonate" is the default impersonation level configured for COM. It is retained in the samples so that they will work equally well across previous platforms, such as Windows NT 4.0.

    List Automatic Services That Are Stopped

    If we now want to see just those services that are set to run automatically but for some reason are not running (for example, a user manually stopped one), we really only need to make one change to the query version of the script, as shown in bold here:

    Set ServiceSet =
    GetObject("winmgmts:{impersonationLevel=impersonate}")
    .ExecQuery("select * from Win32_Service where State=''''Stopped'''' and
    StartMode=''''Auto''''")
    for each Service in ServiceSet
    WScript.Echo Service.Description
    next
    if ServiceSet.Count = 0 Then WScript.echo "No services found meeting query criteria."

    Just as if I were requesting more specific information from an SQL database, all that was needed to get a list of services that are stopped, even though they are configured to run automatically at boot time, was to add a WHERE clause to the query. The specified query narrows the results I get to only those instances that have a State property with the value "Stopped" and a StartMode property set to "Auto."

    Queries can be very simple or quite complex, depending on how specific a set of information you require. Note that I added one more line to the script at the end to catch the case where no instances that match the criteria were returned, which would almost always be the case for this scenario. In order to see an instance get returned by this script, I recommend temporarily stopping the Print Spooler service on your system and then running the script. Before turning off the Print Spooler, you should get the "no matching instances" message, and after you should get a single instance for the Print Spooler service being turned off. Don''''t forget to restart the Print Spooler service when you are done with the test and check that it disappears from the instances returned by the script.

    Listing All Drive Partitions with Less Than 20 Percent Free Space

    In order to determine if a drive partition is low on space, two properties from the Win32_LogicalDisk class need to be retrieved: FreeSpace and Size. With these two pieces of information, the percentage of free space can be determined and those disk partitions below t

    上一页  [1] [2] [3] [4]  下一页


    [系统软件]windows下Apache+php+mysql的安装与配置图解  [办公软件]excel中的VBA中的With语句的使用介绍及实例
    [操作系统]在Windows中玩转Linux操作系统  [操作系统]死马还当活马医:6种方法挽救Windows系统
    [聊天工具]四大更新 Windows Live Msn 8.1评测  [聊天工具]Windows Live Messenger最新0683版亮相_联络工具_…
    [聊天工具]Windows Live Mail招人爱的N个理由_联络工具_Wind…  [聊天工具]Windows Live Mail Desktop多图欣赏_联络工具_Win…
    [聊天工具]OE老了 微软开发新邮件客户端取而代之_联络工具  [聊天工具]Windows Live Messenger中文版试用报告(一)__天极…
    教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台