Session.Invoke 方法

调用方法并返回方法调用的结果。

语法

Session.Invoke( _
  ByVal actionUri, _
  ByVal resourceUri, _
  ByVal parameters, _
  [ ByVal flags ] _
)

parameters

actionUri [in]

要调用的方法的 URI。

resourceUri [in]

要检索的资源的标识符。

此参数可以包含以下项之一:

  • 带或不带 选择器的 URI。 在以下 Visual Basic Scripting Edition (VBScript) 示例中,键由 Win32_Service?Name=winmgmt指定。

    strResourceUri = "http://schemas.microsoft.com/wbem/wsman/1/" _ 
       & "Win32_Service?Name=winmgmt"
    
  • ResourceLocator 对象,其中可能包含选择器、 片段选项

  • WS-Addressing 终结点参考,如 WS-Management Protocol 标准中所述。 有关WS-Management协议的公共规范的详细信息,请参阅 管理规范索引页

parameters [in]

方法输入的 XML 表示形式。 必须提供此字符串,否则此方法将失败。

flags [in, optional]

保留。 必须设置为 0。

返回值

方法输出的 XML 表示形式。

示例

以下 VBScript 代码示例启动Calc.exe进程。 strInputParameters 参数包含 XML 格式的输入参数。 WMI Win32_Process 类的 Create 方法所需的唯一输入参数是要执行的命令行。

Set objWsman = CreateObject( "WSMan.Automation" )
If objWsman is Nothing Then
    WScript.Echo "Failed to create WSMAN Automation object"
    WScript.Quit
End If 

Set objSession = objWsman.CreateSession
If objSession is Nothing Then
    WScript.Echo "Failed to create WSMAN Session object"
    WScript.Quit
End If 

strResource = "http://schemas.microsoft.com/wbem/wsman/1/" & _
    "wmi/root/cimv2/Win32_Process"

strInputParameters = "<p:Create_INPUT " & _
    "xmlns:p=""http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Process"">" & _
    "<p:CommandLine>" & "calc.exe" & _
    "</p:CommandLine>" & _
    "</p:Create_INPUT>"

strOutputParameters = objSession.Invoke( "Create", _
    strResource, strInputParameters )

DisplayOutput( strOutputParameters )

'****************************************************
' Displays WinRM XML message using built-in XSL
'****************************************************
Sub DisplayOutput( strWinRMXml )
    Dim xmlFile, xslFile
    Set xmlFile = CreateObject( "MSXml2.DOMDocument.3.0" ) 
    Set xslFile = CreateObject( "MSXml2.DOMDocument.3.0" )
    xmlFile.LoadXml( strWinRMXml )
    xslFile.Load( "WsmTxt.xsl" )
    Wscript.Echo xmlFile.TransformNode( xslFile ) 
End Sub

以下 VBScript 代码示例调用 Session.Invoke 方法以执行 Win32_ServiceStopService 方法。 StopService 方法没有输入参数。 但是, Invoke 方法需要 参数 参数中的 XML 字符串。

Option Explicit

Dim objWsman
Dim objSession
Dim strResponse
Dim strActionURI
Dim strInputXml
Dim strResourceURI
Dim strMethodName

set objWsman = CreateObject("Wsman.Automation")
set objSession = objWsman.CreateSession
strResourceURI = "http://schemas.microsoft.com/wbem/wsman/1/"_
    & "wmi/root/cimv2/Win32_Service?Name=w32time"
strMethodName = "StopService"
strActionURI = strMethodName                                      

strInputXml = "<p:StopService_INPUT " _
    & "xmlns:p=""http://schemas.microsoft.com/wbem/wsman/1/"_
    & "wmi/root/cimv2/Win32_Service""/>"

strResponse = objSession.Invoke(strMethodName, strResourceURI, strInputXml)

call DisplayOutput(strResponse)
 
strMethodName = "StartService" 
strActionURI = strResourceURI & "/" & strMethodName  
strInputXml = "<p:StartService_INPUT " _
    & "xmlns:p=""http://schemas.microsoft.com/wbem/wsman/1/"_
    & "wmi/root/cimv2/Win32_Service""/>"
strResponse = objSession.Invoke(strMethodName, _
    strResourceURI, strInputXml)

call DisplayOutput(strResponse)

 
'****************************************************
' Displays WinRM XML message using built-in XSL
'****************************************************

Sub DisplayOutput( strWinRMXml )
    Dim xmlFile, xslFile
    Set xmlFile = CreateObject( "MSXml2.DOMDocument.3.0" )    
    Set xslFile = CreateObject( "MSXml2.DOMDocument.3.0" )
    xmlFile.LoadXml( strWinRMXml )
    xslFile.Load( "WsmTxt.xsl" )
    Wscript.Echo xmlFile.TransformNode( xslFile )           
End Sub

要求

要求
最低受支持的客户端
Windows Vista
最低受支持的服务器
Windows Server 2008
标头
WSManDisp.h
IDL
WSManDisp.idl

WSManDisp.tlb
DLL
WSMAuto.dll

另请参阅

会话