Enumerating or Listing All Instances of a Resource

The Session.Enumerate method is the Windows Remote Management approach to obtaining all the instances of a specified resource.

The Session.Enumerate method does not obtain a collection in a SWbemObjectSet object like a SWbemService.ExecQuery call with a WMI query as a parameter (for example, ExecQuery("SELECT * from Win32_LogicalDisk")), or a call to a method like SWbemObject.Instances_. Session.Enumerate and the Enumerator object methods are much more similar to the operation of the scripting TextStream object that is used for reading files as a stream.

To read files as a text stream, you must create the scripting TextStream object and call the TextStream.Readline method to read each line of the file. In a similar way, you can call the Session.Enumerate method to obtain an Enumerator object and call the Enumerator.ReadItem method to get the next item. As is the case when reading from the text file, you can call the Enumerator.AtEndOfStream property to check for whether you have reached the end of the data items.

To enumerate a resource

  1. Create a session.

    Const RemoteComputer = "servername.domain.com"
    Set objWsman = CreateObject( "WSMan.Automation" )
    Set objSession = objWsman.CreateSession( "https://" _
        & RemoteComputer )
    
  2. Construct the URI to identify the resource.

    strResource = "http://schemas.microsoft.com/wbem/wsman/1/" &_
                 "wmi/root/cimv2/Win32_ScheduledJob"
    
  3. Call the Session.Enumerate method. This call starts an enumeration. In WinRM, an enumeration operation does not obtain a collection in the same way that WMI does. Instead, the Session.Enumerate method establishes a WS-Management protocol enumeration context that is maintained in the Enumerator object.

    Set EnumJobs = objSession.Enumerate( strResource )
    
  4. 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 EnumJobs.AtEndOfStream
        NumOfJobs = NumOfJobs + 1
        DisplayOutput( EnumJobs.ReadItem ) 
    Wend
    

The following VBScript code example shows the complete script.

Const RemoteComputer = "servername.domain.com"
Set objWsman = CreateObject( "WSMan.Automation" )
Set objSession = objWsman.CreateSession( "https://" & RemoteComputer )
strResource = "http://schemas.microsoft.com/wbem/wsman/1/" &_
              "wmi/root/cimv2/Win32_ScheduledJob"

Set EnumJobs = objSession.Enumerate( strResource )
NumOfJobs = 0
While Not EnumJobs.AtEndOfStream
    NumOfJobs = NumOfJobs + 1
    DisplayOutput( EnumJobs.ReadItem ) 
Wend
Wscript.Echo "There are " & NumOfJobs & " jobs scheduled."

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

About Windows Remote Management

Using Windows Remote Management

Windows Remote Management Reference