SWbemObject.ExecMethod_ 方法
SWbemObject 对象的ExecMethod_方法执行方法提供程序导出的方法。
此方法在转发到相应提供程序的方法执行时暂停。 然后返回信息和状态。 提供程序而不是 WMI 实现方法。
有关此语法的说明,请参阅 脚本 API 的文档约定。
语法
objOutParams = .ExecMethod_( _
ByVal strMethodName, _
[ ByVal objwbemInParams ], _
[ ByVal iFlags ], _
[ ByVal objwbemNamedValueSet ] _
)
参数
-
strMethodName [in]
-
必需。 对象的方法的名称。
-
objwbemInParams [in, 可选]
-
这是一个 SWbemObject 对象,其中包含要执行的方法的输入参数。 默认情况下,此参数未定义。 有关详细信息,请参阅 构造 InParameters 对象并分析 OutParameters 对象。
-
iFlags [in, 可选]
-
保留,并且必须设置为 0 (0) (如果指定)。
-
objwbemNamedValueSet [in, 可选]
-
通常,它是未定义的。 否则,这是一个 SWbemNamedValueSet 对象,其元素表示可供为请求提供服务的提供程序使用的上下文信息。 支持或需要此类信息的提供程序必须记录已识别的值名称、值的数据类型、允许的值和语义。
返回值
如果此方法成功, 则返回 SWbemObject 对象。 返回的对象包含所执行方法的 out 参数和返回值。
错误代码
ExecMethod_方法完成后,Err 对象可能包含以下列表中的错误代码之一。
-
wbemErrFailed - 2147749889 (0x80041001)
-
错误。
-
wbemErrInvalidClass - 2147749904 (0x80041010)
-
指定的类无效。
-
wbemErrInvalidParameter - 2147749896 (0x80041008)
-
指定的参数无效。
-
wbemErrOutOfMemory - 2147749894 (0x80041006)
-
没有足够的内存来完成操作。
-
wbemErrInvalidMethod - 2147749934 (0x8004102E)
-
请求的方法不可用。
-
wbemErrAccessDenied - 2147749891 (0x80041003)
-
当前用户无权执行该方法。
注解
此方法类似于 SWbemServices.ExecMethod,但它直接对要执行其方法的对象进行操作。 例如,下面的代码示例在Win32_Service中调用 StartService 提供程序方法,并使用直接访问。
oService = GetObject("winmgmts:Win32_Service=Alerter")
iStatus = oService.StartService()
此版本调用 SWbemObject.ExecMethod_ 来执行 StartService 方法。
oService = GetObject("winmgmts:Win32_Service=Alerter")
Set outParam = process.ExecMethod_("StartService")
在无法直接执行方法的情况下,使用 SWbemObject.ExecMethod_ 作为直接访问来直接执行 提供程序方法 的替代方法。 例如,如果方法具有输出参数,则可以将 SWbemObject.ExecMethod_ 与不支持输出参数的脚本语言一起使用。 否则,调用方法的建议方法是使用直接访问。
- SWbemObject.ExecMethod_方法假定由 SWbemObject 表示的对象包含要执行的方法。 相比之下, SWbemServices.ExecMethod 需要对象路径。 如果已获取要执行其方法的对象,请使用 SWbemObject.ExecMethod_ 。
示例
以下示例演示 ExecMethod 方法。该脚本创建一个Win32_Process对象,该对象表示运行记事本的进程。 有关说明异步执行的相同操作的脚本的详细信息,请参阅 SWbemObject.ExecMethodAsync_。 有关使用直接访问的示例,请参阅 类Win32_Process中的 Create 方法 。 有关使用 SWbemServices 对象执行相同操作的示例,请参阅 SWbemServices.ExecMethod。
' Connect to WMI and obtain a Win32_Process object.
' This is also an SWbemObject object.
Set oProcess = GetObject("winmgmts:Win32_Process")
' Create the SWbemMethod.InParameters
' object to hold the input parameter needed
' for the Win32_Process.Create method call.
' The oProcess.Methods_("Create") call
' obtains a class object that defines
' the correct input parameters
' for the Win32_Process.Create call.
' The InParameters object is an
' SWbemObject object so SWbemObject.SpawnInstance_
' can be called to create it.
Set oInParams = oProcess.Methods_("Create"). _
InParameters.SpawnInstance_
' Specify the name of the process to be run.
oInParams.CommandLine = "Notepad.exe"
Set oOutParams = oProcess.ExecMethod_("Create", oInParams)
If oOutParams.ReturnValue = 0 Then
wscript.echo "Create method executed successfully"
Else
' If the Create method failed to execute,
' an empty OutParameters object is returned.
If IsNull(oOutParams.ReturnValue) Then
wscript.echo "Create method failed to execute."
Else
wscript.echo "Create method executed but had error" _
& "0x" & hex(oOutParams.ReturnValue)
End If
End If
要求
要求 | 值 |
---|---|
最低受支持的客户端 |
Windows Vista |
最低受支持的服务器 |
Windows Server 2008 |
标头 |
|
类型库 |
|
DLL |
|
CLSID |
CLSID_SWbemObject |
IID |
IID_ISWbemObject |