Bewerken

Delen via


Querying for Specific Instances of a Resource

The call to Session.Enumerate has optional parameters that narrow the enumeration into a query. Because the WinRM Scripting API and the WinRM C++ API are closely modeled on the underlying WS-Management protocol, the parameters use the same terminology for querying as the protocol—filter and filter dialect.

You can either use the filter and dialect parameters of Session.Enumerate or you can construct and supply a ResourceLocator object and the AddSelector method, but you cannot do both.

This procedure executes a query for network adapters that have TCP/IP bound and enabled. The query requests all the instances of Win32_NetworkAdapterConfiguration that have the IpEnabled property set to True. Except for the addition of the filter and dialect, the query is handled like a simple enumeration.

In this example, the resource name for the Resource constant is represented by an asterisk "*" because the class name, Win32_NetworkAdapterConfiguration, is already mentioned in the strFilter string.

To query for specific instances of a resource

  1. For ease-of-reading, define URIs as constants.

    Const RemoteComputer = "servername.domain.com"
    Const Resource = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*"
    Const Dialect = "http://schemas.microsoft.com/wbem/wsman/1/WQL"
    
  2. Create a session.

    Set objWsman = CreateObject("Wsman.Automation")
    Set objSession = objWsman.CreateSession("https://" & RemoteComputer)
    
  3. Construct the filter string. Windows Remote Management supports WQL as the filter dialect.

    strFilter = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IpEnabled=TRUE"
    
  4. Set any required enumeration constants in the flags parameter.

    Be aware that if the flags include the Enumeration Constants WSManFlagHierarchyDeepBasePropsOnly or WSManFlagHierarchyShallow then WinRM service returns the error code ERROR_WSMAN_POLYMORPHISM_MODE_UNSUPPORTED.

  5. Call the Session.Enumerate method. This call starts an enumeration. The Session.Enumerate method establishes a WS-Management protocol enumeration context, maintained in the Enumerator object.

    Set objResultSet = objSession.Enumerate(Resource, strFilter, Dialect)
    
  6. Call the Enumerator.ReadItem method to obtain the next item of the results. In WS-Management protocol, this corresponds to the pull operation. Use the Enumerator.AtEndOfStream method as a control to know when to stop reading.

    While Not objResultSet.AtEndOfStream
        DisplayOutput(objResultSet.ReadItem)
    Wend
    

The following VBScript code example shows the complete script.

Const RemoteComputer = "servername.domain.com"
Const Resource = "http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*"
Const Dialect = "http://schemas.microsoft.com/wbem/wsman/1/WQL"

Set objWsman = CreateObject("Wsman.Automation")
Set objSession = objWsman.CreateSession("https://" & RemoteComputer)

strFilter = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IpEnabled=TRUE"

Set objResultSet = objSession.Enumerate(Resource, strFilter, Dialect)

While Not objResultSet.AtEndOfStream
    DisplayOutput(objResultSet.ReadItem)
Wend

'****************************************************
' 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

Using Windows Remote Management

Enumerating or Listing All of the Instances of a Resource

ResourceLocator