Bewerken

Delen via


SWbemObject.Delete_ method

The Delete_ method of the SWbemObject object deletes either the current class or the current instance. If a dynamic provider supplies the class or instance, it is sometimes not possible to delete this object unless the provider supports class or instance deletion. For an explanation of this syntax, see Document Conventions for the Scripting API.

Syntax

SWbemObject.Delete_( _
  [ ByVal iFlags ], _
  [ ByVal objwbemNamedValueSet ] _
)

Parameters

iFlags [in, optional]

Reserved and must be 0 (zero) if specified.

objwbemNamedValueSet [in, optional]

This parameter is typically undefined. Otherwise, this is an SWbemNamedValueSet object whose elements represent the context information that can be used by the provider that is servicing the request. A provider that supports or requires such information must document the recognized value names, data type of the value, allowed values, and semantics.

Return value

This method does not return a value.

Error codes

After completion of the Delete_ method, the Err object may contain one of the error codes in the following list.

wbemErrAccessDenied - 2147749891 (0x80041003)

Current context does not have adequate security rights to delete the object.

wbemErrFailed - 2147749889 (0x80041001)

Unspecified error.

wbemErrInvalidClass - 2147749904 (0x80041010)

Specified class does not exist.

wbemErrInvalidOperation - 2147749910 (0x80041016)

Object cannot be deleted.

wbemErrNotFound - 2147749890 (0x80041002)

Object did not exist.

wbemErrOutOfMemory - 2147749894 (0x80041006)

Not enough memory to complete the operation.

Remarks

The Delete_ method fails if a new instance of SWbemObject is created, but no value is supplied for the key property. Windows Management Instrumentation (WMI) generates a globally unique identifier (GUID) value automatically, but a GUID value is not accepted by SWbemObject.Delete_. In this case, SWbemServices.Delete, which uses the object path works. Note that an SWbemObjectPath object is returned by the SWbemObject.Put_ method after an object is committed to WMI.

Examples

The following example creates a new class; adds a key property; writes the new class to the repository; and displays the path of the new class object. The script then spawns an instance of the new class; writes it; and displays the path. Note that the script deletes both the class and its instances from the repository by simply deleting the class.

On Error Resume Next
wbemCimtypeString = 8             ' String datatype
Set objSWbemService = GetObject("Winmgmts:root\default")
Set objClass = objSWbemService.Get()
objClass.Path_.Class = "NewClass"

' Add a property
' String property
objClass.Properties_.add "PropertyName", wbemCimtypeString 
' Make the property a key property 
objClass.Properties_("PropertyName").Qualifiers_.Add "key", TRUE

' Write the new class to the root\default namespace in the repository
Set objClassPath = objClass.Put_
wscript.echo objClassPath.Path

'Create an instance of the new class using SWbemObject.SpawnInstance
Set objNewInst = GetObject( _
    "Winmgmts:root\default:NewClass").SpawnInstance_

objNewInst.PropertyName = "My Instance"

' Write the instance into the repository
Set objInstancePath = objNewInst.Put_
wscript.echo objInstancePath.Path

' Remove the new class and instance from the repository
objClass.Delete_()
If Err <> 0 Then
    WScript.Echo Err.Number & "    " & Err.Description 
Else
    WScript.Echo "Delete succeeded"
End If

' Release SwbemServices object
Set objSWbemService = Nothing

Requirements

Requirement Value
Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Header
Wbemdisp.h
Type library
Wbemdisp.tlb
DLL
Wbemdisp.dll
CLSID
CLSID_SWbemObject
IID
IID_ISWbemObject