從 WMI 集合中移除單一專案

存取集合的主要用途之一是從集合中移除專案。 您可以使用 對 SWbemPropertySet.Remove 方法的呼叫,從集合中移除專案。 這個方法不適用於 SWbemObjectSetSWbemMethodSet

專案會依名稱從 SWbemPropertySetSWbemQualifierSetSWbemNamedValueSet中移除。 不過, SWbemRefresher 中的專案會由 index 移除,而 SWbemPrivilegeSet 中的專案則由代表權限名稱的常數移除。

從集合中移除專案

  • 下列程式碼範例示範如何使用 對 SWbemPropertySet.Remove 方法的呼叫來移除專案。

    oclass.Properties_.Remove "Prop2"
    

    下列範例會在 root\default 命名空間中建立名為 「NewClass」 的新類別,並在其中新增三個屬性。 然後,腳本會使用上一個範例中的程式碼來刪除第二個屬性。

    ' 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
    

如需詳細資訊,請參閱 操作類別和實例資訊存取集合,以及 從集合中移除多個專案