Bewerken

Delen via


Removing a Single Item from a WMI Collection

One of the main purposes of accessing a collection is to remove an item from the collection. You can remove an item from a collection with a call to the SWbemPropertySet.Remove method. This method is not available for SWbemObjectSet or SWbemMethodSet.

Items are removed by name from SWbemPropertySet, SWbemQualifierSet, and SWbemNamedValueSet. However, items in SWbemRefresher are removed by index and from SWbemPrivilegeSet by the constant that represents the privilege name.

To remove an item from a collection

  • The following code example shows how to remove the item with a call to the SWbemPropertySet.Remove method.

    oclass.Properties_.Remove "Prop2"
    

    The following example creates a new class named "NewClass" in the root\default namespace and adds three properties to it. The script then uses the code from the previous example to delete the second property.

    ' Obtain an empty class and name it
    Const WBEM_CIMTYPE_STRING = 8
    Set objSWbemService = GetObject("winmgmts:root\default")
    Set objClass = objSWbemService.get()
    Wscript.Echo "Creating class NewClass"
    objClass.Path_.Class = "NewClass"
    
    ' Add three properties 
    For i = 1 to 3
        objClass.Properties_.Add "Prop" & i, WBEM_CIMTYPE_STRING
    Next
    Getprops()
    
    ' Remove the Prop2 property
    objClass.Properties_.Remove "Prop2"
    Wscript.Echo "Second property removed "
    Getprops()
    
    ' Write the changes to the class back
    objClass.Put_
    
    Sub Getprops()
        Wscript.Echo "Number of Properties = " _
            & objClass.Properties_.Count
        For Each prop in objClass.Properties_
            Wscript.Echo prop.name
        Next
    End Sub
    

For more information, see Manipulating Class and Instance Information, Accessing a Collection, and Removing Multiple Items from a Collection.