调用 WMI 方法Calling a WMI Method

WMI 在 COM API脚本编写 API 中提供了方法,用于获取信息或操作企业系统中的对象。WMI supplies methods in the COM API and the scripting API to obtain information or manipulate objects in an enterprise system. 例如,WMI 脚本方法 SWbemServices.ExecQuery 查询数据。For example, the WMI scripting method SWbemServices.ExecQuery queries for data. 提供程序还在其注册的类中定义了方法。Providers also have methods defined in the classes they register. 示例是 win32 提供程序提供的 Win32 _ 逻辑磁盘方法 ChkdskScheduleAutoChkExamples are the Win32_LogicalDisk methods Chkdsk and ScheduleAutoChk supplied by the Win32 provider.

本主题中讨论了以下部分:The following sections are discussed in this topic:

WMI 方法与提供程序方法的比较WMI Methods Compared to Provider Methods

通过将 WMI 方法 调用与 提供程序方法 调用结合使用,可检索和操作有关企业的信息。By using WMI method calls combined with provider method calls, you can retrieve and manipulate information about your enterprise. 有关详细信息,请参阅 调用 WMI 方法调用提供程序方法For more information, see Calling a WMI Method and Calling a Provider Method.

WMI 脚本对象 SWbemObject 的方法有一种特殊状态,因为它们可应用于任何 WMI 数据类。The methods of the WMI scripting object SWbemObject have a special status because they can apply to any WMI data class. 有关详细信息,请参阅 SWbemObject 的脚本编写For more information, see Scripting with SWbemObject.

下面的代码示例调用了 WMI 和提供程序方法。The following code example calls both WMI and provider methods.

以下 WMI 和提供程序方法位于 wmi 的脚本编写 API中:The following WMI and provider methods are located in the Scripting API for WMI:

可以在 " Win32 _ 服务" 的 "返回代码" 部分中查找可能出现在 "返回" 中的代码。You can look up the code that may appear in "Return" in the Return Codes section for Win32_Service.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colServices = objWMIService.ExecQuery ("Select * from Win32_Service where Name='Alerter'")
For Each objService in colServices
    Return = objService.StopService()
    If Return <> 0 Then
        Wscript.Echo "Failed " &VBNewLine & "Error code = " & Return 
    Else
       WScript.Echo "Succeeded"
    End If
Next

$colServices= Get-WmiObject -Class Win32_Service -Filter 'Name = &quot;Alerter&quot;'
foreach ($objService in $colServices)
{
    $objService.StopService()
}

WMI 中的 Method-Calling 模式Method-Calling Modes in WMI

半同步调用模式通常提供安全性与性能之间的最佳平衡。The semisynchronous calling mode usually provides the best balance between security and performance.

有关每种可能模式的详细信息,请参阅以下内容:For more information about each of the possible modes, see the following:

同步模式Synchronous Mode

当程序或脚本暂停,直到方法调用返回 SWbemObjectSet collection 对象时,才会发生同步模式。Synchronous mode occurs when the program or scripts pauses until the method call returns a SWbemObjectSet collection object. WMI 在将集合对象返回给调用程序或脚本之前,将在内存中生成此集合。WMI builds this collection in memory before returning the collection object to the calling program or script.

同步模式可能对运行程序或脚本的计算机上的程序或脚本性能产生不利影响。Synchronous mode can have an adverse effect of program or script performance on the computer running the program or script. 例如,从事件日志中同步检索上千个事件可能需要很长时间,并且会占用大量内存,因为 WMI 会从每个事件创建一个对象,然后将这些对象放入集合中,然后再将该集合传递给方法。For example, synchronously retrieving thousands of events from the event log can take a long time and use a lot of memory because WMI creates an object from each event and then puts those objects into a collection before passing the collection to the method.

只应调用在同步模式下不返回大型数据集的方法。You should only call methods that do not return large data sets in synchronous mode. 可以安全地在同步模式下调用以下 SWbemServices 方法:The following SWbemServices methods can be safely called in synchronous mode:

名称中不包含单词 "Async" 的任何 SWbemServices方法都可以通过在 iFlags 参数中设置 wbemFlagReturnWhenComplete 值,在同步模式下调用。Any SWbemServices methods without the word, "Async" in the name can be called in synchronous mode by setting the wbemFlagReturnWhenComplete value in the iFlags parameter.

异步模式Asynchronous Mode

当程序或脚本在调用方法后继续运行时,将发生异步模式。Asynchronous mode occurs when the program or script continues to run after calling the method. 在创建每个对象时,WMI 会将方法中的所有对象返回到 SWbemSink 对象。WMI returns all objects from the method to a SWbemSink object as each object is created. 调用程序或脚本必须具有 SWbemSink 对象和 SWbemSink OnObjectReady 事件处理程序才能处理返回的对象。The calling program or script must have an SWbemSink object and an SWbemSink.OnObjectReady event handler to process the returned objects. 有关为异步模式创建事件处理程序的详细信息,请参阅 接收 WMI 事件For more information about creating an event handler for asynchronous mode, see Receiving a WMI Event.

尽管此模式不会影响同步模式的性能和资源,但异步模式可能会造成严重的安全风险,因为存储在 SWbemSink 对象中的结果可能不来自调用程序或脚本。While this mode does not have the performance and resource penalty of synchronous mode, asynchronous mode can create serious security risks because the results stored in the SWbemSink object may not come from the calling program or script. WMI 降低了 SWbemSink 对象上的身份验证级别,直到该方法成功。WMI lowers the authentication level on the SWbemSink object until the method succeeds. 有关如何缓解这些安全风险的详细信息,请参阅 设置异步调用的安全性For more information about how to mitigate these security risks, see Setting Security on an Asynchronous Call.

用单词 Async 追加的方法是异步模式的方法。Methods appended with the word Async are methods for asynchronous mode. 以下方法是异步调用:The following methods are asynchronous calls:

有关异步模式的详细信息,请参阅:For more information about asynchronous mode, see:

半同步模式Semisynchronous Mode

半同步模式类似于异步模式,因为在调用方法后,程序或脚本将继续运行。Semisynchronous mode is similar to asynchronous mode in that the program or script continues to run after calling the method. 在半同步模式下,WMI 将在后台检索对象,因为脚本或程序将继续运行。In semisynchronous mode, WMI retrieves the objects in the background as your script or program continues to run. WMI 在创建对象后,返回返回给调用方法的每个对象。WMI returns each object returned to the calling method right after the object is created.

因为 WMI 管理对象,所以半同步模式比异步模式更安全。Because WMI manages the object, semisynchronous mode is more secure than asynchronous mode. 但是,如果你使用的半同步模式包含1000个以上的实例,则实例检索可以独占可用资源,这可能会降低程序或脚本与使用程序或脚本的计算机的性能。However, if you use semisynchronous mode with more than 1,000 instances, instance retrieval can monopolize the available resources, which can degrade the performance of the program or script and the computer using the program or script. 在释放内存之前,每个对象都占用必要的资源。Each object takes up the necessary resources until the memory is released.

若要解决此问题,可以调用方法,将 iFlags 参数设置为 wbemFlagForwardOnlyWBEMFLAGRETURNIMMEDIATELY 标志,以指示 WMI 返回只进 SWbemObjectSetTo work around this condition, you can call the method with the iFlags parameter set with the wbemFlagForwardOnly and wbemFlagReturnImmediately flags to instruct WMI to return a forward-only SWbemObjectSet. 只进 SWbemObjectSet 消除了在枚举对象后释放内存导致的大型数据集导致的性能问题。A forward-only SWbemObjectSet eliminates the performance problem caused by a large data set by releasing the memory after the object is enumerated.

不能在同步或异步模式下调用的任何 SWbemServices 方法均以半同步模式调用。Any SWbemServices method that cannot be called in either synchronous or asynchronous mode is called in semisynchronous mode.

在半同步模式下调用以下方法:The following methods are called in semisynchronous mode:

有关半同步模式的详细信息,请参阅 使用 c + + 进行半同步调用使用 VBScript 进行半同步调用For more information about semisynchronous mode, see Making a Semisynchronous Call with C++ and Making a Semisynchronous Call with VBScript.

提高枚举性能Improving Enumeration Performance

用 SWbemObject 编写脚本Scripting with SWbemObject

WbemFlagEnumWbemFlagEnum