Export (0) Print
Expand All

SWbemObjectSet object

An SWbemObjectSet object is a collection of SWbemObject objects. For more information, see Accessing a Collection. This object cannot be created by the VBScript CreateObject call.

You can get an SWbemObjectSet object by calling any of the following methods or their asynchronous equivalents:

Note  The SWbemObjectSet object does not support the optional Add and Remove collection methods.

Note  Because the call-back to the sink might not be returned at the same authentication level as the client requires, it is recommended that you use semisynchronous instead of asynchronous communication. For more information, see Calling a Method.

Members

The SWbemObjectSet object has these types of members:

Methods

The SWbemObjectSet object has these methods.

MethodDescription
Item

Retrieves an SWbemObject object from the collection. This is the default method of the object.

 

Properties

The SWbemObjectSet object has these properties.

PropertyAccess typeDescription

Count

Read-only

The number of items in the collection.

Security_

Read-only

Used to read or change the security settings.

 

Remarks

An SWbemObjectSet is a collection of zero or more SWbemObject objects. Each SWbemObject in a SWbemObjectSet can represent one of two things:

  • An instance of a WMI-managed resource.
  • An instance of a class definition.

The most common use of this class in WMI is as the return value for an ExecQuery or InstancesOf call, as described in the following code sample:

strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServices = objSWbemServices.ExecQuery("SELECT State FROM Win32_Service")
For Each objService In colServices
    Wscript.Echo objService.Name, objService.State
Next

For the most part, the only thing you will ever do with an SWbemObjectSet is enumerate all the objects contained within the collection itself. However, SWbemObjectSet does include a property Count that can be useful in system administration scripting. As the name implies, Count tells you the number of items in the collection. For example, this script retrieves a collection of all the services installed on a computer and then echoes the total number of services found:

For more information on how to use this class, see Enumerating WMI.

Examples

The following VBScript code sample illustrates how SWbemObjectSet collections are manipulated.

On Error Resume Next

Set Disks = GetObject("winmgmts:").InstancesOf ("CIM_LogicalDisk")

WScript.Echo "There are", Disks.Count, " Disks"

Set Disk = Disks("Win32_LogicalDisk.DeviceID=""C:""")
WScript.Echo Disk.Path_.Path

if Err <> 0 Then
 WScript.Echo Err.Description
 Err.Clear
End if

The following Perl code sample illustrates how SWbemObjectSet collections are manipulated.


use strict;
use Win32::OLE;

my ($disks,$disk);

eval { $disks = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2")->
   InstancesOf("CIM_LogicalDisk"); };
unless($@)
{
 print "\nThere are ", $disks->{Count}, " Disks \n";

 eval { $disk = $disks->Item("Win32_LogicalDisk.DeviceID=\"C:\""); };
 unless($@)
 {
  print $disk->{Path_}->{Path}, "\n";
 }
 else
 {
  print STDERR Win32::OLE->LastError, "\n";
 }
}
else
{
 print STDERR Win32::OLE->LastError, "\n";
}

Requirements

Minimum supported client

Windows Vista

Minimum supported server

Windows Server 2003

Type library

Wbemdisp.tlb

DLL

Wbemdisp.dll

See also

Scripting API Objects

 

 

Show:
© 2014 Microsoft