使用 WMIUsing WMI

可以通过客户端应用程序和脚本使用 WMI。You can use WMI from client applications and scripts. 它提供一个基础结构,使你能够轻松发现和执行管理任务。It provides an infrastructure that makes it easy to both discover and perform management tasks. 此外,你可以通过创建自己的 WMI 提供程序来添加到一组可能的管理任务。In addition, you can add to the set of possible management tasks by creating your own WMI providers.

备注

用于编写应用程序和脚本的 WMI 的下一代版本通过 Windows 管理基础结构 (MI) 提供。The next-generation version of WMI for writing applications and scripts is available through the Windows Management Infrastructure (MI). 有关详细信息,请参阅 MI 提供程序和客户端For more information, see MI Providers and Clients.

以下是本节中要讨论的主题:The following topics are discussed in this section:

从 WMI 获取数据Obtaining Data from WMI

下面的过程介绍如何通过编写脚本或应用程序从 WMI 中获取数据。The following procedure describes how to obtain data from WMI by writing a script or application.

通过编写脚本或应用程序从 WMI 获取数据To obtain data from WMI by writing a script or application

  1. 确定要使用的语言。Decide which language to use. 有关脚本编写的详细信息,请参阅 创建 WMI 脚本For more information about scripting, see Creating a WMI Script. 有关 c + + 的详细信息,请参阅 使用 c + + 创建 WMI 应用程序For more information about C++, see Creating a WMI Application Using C++. 有关 c # 或 WMI .NET 的详细信息,请参阅 WMI .Net 概述For using more information about C# or WMI .NET, see WMI .NET Overview.

    您可以以多种语言查看或操作 WMI 数据。You can view or manipulate WMI data in many languages. 下表列出了一些主题,这些主题介绍如何使用脚本语言和应用程序语言来获取数据。The following table lists the topics that describe how to use the scripting and application languages to obtain data.

    应用程序语言Application language 主题Topic
    在 Microsoft ActiveX 脚本托管中编写的脚本,包括 Visual Basic Scripting Edition (VBScript) 和 PerlScripts written in Microsoft ActiveX script hosting, including Visual Basic Scripting Edition (VBScript) and Perl
    WMI 脚本编写 APIScripting API for WMI.
    首先要 创建 WMI 脚本Start with Creating a WMI Script.
    有关脚本代码示例,请参阅 脚本和应用程序的 WMI 任务 和 TechNet ScriptCenter 脚本存储库。For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.
    Windows PowerShellWindows PowerShell
    Windows PowerShell 入门Getting Started with Windows PowerShell
    WMI PowerShell Cmdlet,如 get-wmiobjectWMI PowerShell Cmdlets, such as Get-WmiObject.
    Visual Basic 应用程序Visual Basic applications
    WMI 脚本编写 APIScripting API for WMI.
    Active Server PagesActive Server Pages
    WMI 脚本编写 APIScripting API for WMI.
    开始为 WMI 创建 Active Server 页面Start with Creating Active Server Pages for WMI.
    C++ 应用程序C++ applications
    用于 WMI 的 COM APICOM API for WMI.
    首先,使用 c + + 和Wmi c + + 应用程序示例创建 wmi 应用程序 (包含) 的示例。Start with Creating a WMI Application Using C++ and WMI C++ Application Examples (contains examples).
    .NET Framework 用 c # 编写的应用程序,Visual Basic .NET 或 J#.NET Framework applications written in C#, Visual Basic .NET, or J#
    的类。Classes in the Microsoft.Management.Infrastructure namespace.
    [!Note]
    System.web 是涵盖 WMI 托管代码的原始命名空间。 System.Management was the original namespace that covered managed code for WMI. 但是, 系统管理 的基础技术通常比更慢,并且不能扩展和 管理基础结构However, the underlying technology for System.Management is generally slower than, and does not scale as well as, Microsoft.Management.Infrastructure. 因此,不建议对新项目使用 " 系统管理 "。As such, it is not recommended that you use System.Management for new projects. (有关 系统管理的详细信息,请参阅 WMI .net 概述。 ) (For more information on System.Management, see WMI .NET Overview.)

  2. 确保与远程计算机的连接正常运行。Ensure that your connections to remote computers work.

    有关详细信息,请参阅 连接到远程计算机上的 WMIFor more information, see Connecting to WMI on a Remote Computer.

  3. 连接到远程计算机上的 WMI 要求正确的安全设置,如 维护 WMI 安全性中所述。Connecting to WMI on remote computers requires the correct security settings, as explained in Maintaining WMI Security. 下表列出了介绍如何配置脚本和应用程序语言的安全设置的主题。The following table lists the topics that describe how to configure security settings with the scripting and application languages.

    语言Language 主题Topic
    任何语言的脚本、Visual Basic 应用程序Scripts in any language, Visual Basic applications
    使用 VBScript 设置默认进程安全级别Setting the Default Process Security Level Using VBScript
    Active Server PagesActive Server Pages
    为 WMI ASP 脚本配置 IIS 5 和更高版本Configuring IIS 5 and Later for WMI ASP Scripting
    C++C++
    使用 c + + 设置默认进程安全级别设置 IWbemServices 和其他代理的安全性Setting the Default Process Security Level Using C++ and Setting the Security on IWbemServices and Other Proxies
  4. 连接到 WMI 后,可以通过查询和枚举获取数据。After connecting to WMI, you can obtain data through queries and enumerations.

    有关详细信息,请参阅 操作类和实例信息用 WQL 进行查询For more information, see Manipulating Class and Instance Information and Querying with WQL.

  5. 注册表数据通过 WMI 提供,你可以创建新的密钥和值,也可以修改现有的密钥和值。Registry data is available through WMI and you can create new keys and values or modify existing ones.

    有关详细信息,请参阅 修改系统注册表For more information, see Modifying the System Registry.

  6. 你可以通过 WMI 订阅事件通知,无论是在系统重新启动还是永久地进行。You can subscribe to event notifications through WMI, either temporarily between system reboots or permanently.

    有关详细信息,请参阅 监视事件接收 WMI 事件For more information, see Monitoring Events and Receiving a WMI Event.

  7. 系统的性能计数器数据通过 WMI 提供。Performance counter data for a system is available through WMI.

    系统性能库计数器会转换为 WMI 类。The system performance library counters are converted to WMI classes. 有关详细信息,请参阅 监视性能数据For more information, see Monitoring Performance Data.

  8. 脚本和应用程序的 WMI 任务 介绍了如何通过 wmi 执行许多管理任务。WMI Tasks for Scripts and Applications describes how to do many administrative tasks with WMI.

向 WMI 提供数据Providing Data to WMI

下面的过程介绍如何通过编写提供程序向 WMI 提供数据。The following procedure describes how to supply data to WMI by writing a provider.

通过编写提供程序向 WMI 提供数据To supply data to WMI by writing a provider

  • 确定要写入的提供程序的类型。Decide on the type of provider to write.

    不能在 VBScript 中编写 WMI 提供程序。You cannot write a WMI provider in VBScript. 但是,您可以采用多种其他方法编写 WMI COM 提供程序:However, you can take several other approaches to writing a WMI COM provider:

    • 使用 Visual Studio 中的 WMI ATL 向导。Using the WMI ATL Wizard in Visual Studio.

      此方法创建非托管 COM 提供程序。This approach creates an unmanaged COM provider. 有关详细信息,请参阅 添加 Wmi 实例提供程序添加 Wmi 事件提供程序For more information, see Adding a WMI Instance Provider and Adding a WMI Event Provider.

    • 直接在任何集成开发环境中使用 COM。Using COM directly in any integrated development environment.

      此方法创建非托管 COM 提供程序。This approach creates an unmanaged COM provider.

    • 在 .NET Framework 中使用 WMI 创建托管代码提供程序。Using WMI in the .NET Framework to create a managed code provider.

      此方法会创建托管代码提供程序。This approach creates a managed code provider. 托管代码提供程序可以用任何 .NET Framework 语言编写,比 WMI COM 提供程序更简单,并且可以从 WMI 基于 CIM的类(如 Win32 类)获取数据。Managed code providers can be written in any .NET Framework language, are simpler to write than WMI COM providers, and can obtain data from the WMI CIM-based classes such as Win32 Classes. 但 .NET Framework WMI 提供程序有一些限制。However, the .NET Framework WMI provider has some limitations. 有关详细信息,请参阅 使用 WMI 管理应用程序For more information, see Managing Applications Using WMI.

    • 不建议使用 提供程序框架类Using the provider framework classes is not recommended.

      提供程序框架已被 WMI ATL 向导取代,直接使用 COM 或 .NET Framework 提供程序。The provider framework has been superseded by the WMI ATL wizards, using COM directly, or .NET Framework providers. 不再建议使用提供程序框架类创建 WMI COM 提供程序。Creating a WMI COM provider with the provider framework classes is no longer recommended. 下表列出的主题介绍了如何使用 COM 或 .NET Framework 提供程序。The following table lists the topics that describe how to use COM or .NET Framework providers.

    提供程序Provider 主题Topic
    与 WMI 在同一进程中的 COM 提供程序COM provider in the same process as WMI
    向 WMI 提供数据Providing Data to WMI
    COM 分离的提供程序COM decoupled provider
    在应用程序中包含提供程序Incorporating a Provider in an Application
    C # 或 Visual Basic.NET 中的 .NET Framework 提供程序.NET Framework provider in C# or Visual Basic.NET
    使用 WMI 管理应用程序Managing Applications Using WMI

WMI 的重要任务Important Tasks for WMI

以下主题提供有关使用 WMI 监视和控制企业组件的信息。The following topics provide information about using WMI to monitor and control enterprise components.

主题Topic 描述Description
脚本和应用程序的 WMI 任务WMI Tasks for Scripts and Applications
描述如何在执行常见计算机和网络管理任务的脚本和应用程序中查找要使用的正确 WMI 类和过程,如为远程计算机添加新的打印机连接或查找计算机上的所有已安装的修补程序。Describes how to find the correct WMI class and procedures to use in scripts and applications that perform common computer and network administration tasks, such as adding a new printer connection for a remote computer or finding all the installed hotfixes on a computer.
创建 WMI 应用程序或脚本Creating a WMI Application or Script
适用于 ActiveX 对象的任何脚本语言(如 VBScript 或 Perl)都可访问 WMI 数据。Any scripting language, such as VBScript or Perl, that works with ActiveX objects can access WMI data. 应用程序可以使用 适用于 wmi 的 COM API 或 Visual Basic 中使用 wbemdisp.tlb类型库wmi 的脚本编写 API访问 c + + 中的 wmi。Applications can access WMI in C++, using the COM API for WMI or in Visual Basic, using the Wbemdisp.tlbtype library and the Scripting API for WMI.
连接到远程计算机上的 WMIConnecting to WMI on a Remote Computer
描述脚本、应用程序和提供程序如何与远程计算机上的 WMI 建立连接,以获取数据或控制硬件和软件。Describes how scripts, applications, and providers can establish connections to WMI on remote computers to obtain data or control hardware and software.
使用 Windows PowerShell 连接到远程计算机上的 WMIConnecting to WMI on a Remote Computer by Using Windows PowerShell
描述如何使用 Windows PowerShell 在远程计算机上建立与 WMI 的连接,以获取数据或控制硬件和软件。Describes how to use Windows PowerShell to establish connections to WMI on remote computers to obtain data or to control hardware and software.
监视事件Monitoring Events
介绍如何通过创建临时或永久 WMI 事件使用者来获取事件通知。Describes how to get event notifications by creating temporary or permanent WMI event consumers.
向 WMI 提供数据Providing Data to WMI
WMI 通过从提供程序获取客户端脚本和应用程序向其提供动态管理数据。WMI supplies dynamic management data to client scripts and applications by obtaining it from providers.
在64位计算机上获取和提供数据Getting and Providing Data on a 64-bit Computer
描述如何在64位系统上访问非默认提供程序和提供程序编写器的注意事项。Describes how to access nondefault providers and considerations for provider writers on 64-bit systems.