Bagikan melalui


Menghapus Item Tunggal dari Koleksi WMI

Salah satu tujuan utama mengakses koleksi adalah untuk menghapus item dari koleksi. Anda dapat menghapus item dari koleksi dengan panggilan ke metode SWbemPropertySet.Remove . Metode ini tidak tersedia untuk SWbemObjectSet atau SWbemMethodSet.

Item dihapus berdasarkan nama dari SWbemPropertySet, SWbemQualifierSet, dan SWbemNamedValueSet. Namun, item di SWbemRefresher dihapus oleh indeks dan dari SWbemPrivilegeSet oleh konstanta yang mewakili nama hak istimewa.

Untuk menghapus item dari koleksi

  • Contoh kode berikut menunjukkan cara menghapus item dengan panggilan ke metode SWbemPropertySet.Remove .

    oclass.Properties_.Remove "Prop2"
    

    Contoh berikut membuat kelas baru bernama "NewClass" di namespace root\default dan menambahkan tiga properti ke dalamnya. Skrip kemudian menggunakan kode dari contoh sebelumnya untuk menghapus properti kedua.

    ' 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
    

Untuk informasi selengkapnya, lihat Memanipulasi Informasi Kelas dan Instans, Mengakses Koleksi, dan Menghapus Beberapa Item dari Koleksi.