SWbemObject.Delete_ 方法

SWbemObject物件的Delete_方法會刪除目前類別或目前的實例。 如果動態提供者提供 類別或實例,除非提供者支援類別或實例刪除,否則有時無法刪除此物件。 如需此語法的說明,請參閱 腳本 API 的檔慣例

語法

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

參數

iFlags [in, optional]

如果指定,則保留且必須是 0 (零) 。

objwbemNamedValueSet [in, optional]

此參數通常未定義。 否則,這是 SWbemNamedValueSet 物件,其元素代表服務要求的提供者可以使用的內容資訊。 支援或需要這類資訊的提供者必須記載已辨識的值名稱、值的資料類型、允許的值和語意。

傳回值

這個方法不會傳回值。

錯誤碼

完成 Delete_ 方法之後, Err 物件可能會包含下列清單中的其中一個錯誤碼。

wbemErrAccessDenied - 2147749891 (0x80041003)

目前的內容沒有足夠的安全性許可權可刪除物件。

wbemErrFailed - 2147749889 (0x80041001)

未指定的錯誤。

wbemErrInvalidClass - 2147749904 (0x80041010)

指定的類別不存在。

wbemErrInvalidOperation - 2147749910 (0x80041016)

無法刪除物件。

wbemErrNotFound - 2147749890 (0x80041002)

物件不存在。

wbemErrOutOfMemory - 2147749894 (0x80041006)

記憶體不足,無法完成作業。

備註

如果建立SWbemObject的新實例,但索引鍵屬性沒有提供任何值,則Delete_方法會失敗。 Windows Management Instrumentation (WMI) 會自動產生全域唯一識別碼 (GUID) 值,但 SWbemObject.Delete_不接受 GUID 值。 在此情況下, SWbemServices.Delete會使用物件路徑運作。 請注意,在物件認可至 WMI 之後,SWbemObject.Put_方法會傳回SWbemObjectPath物件。

範例

下列範例會建立新的類別;加入索引鍵屬性;將新類別寫入存放庫;和 會顯示新類別物件的路徑。 然後腳本會繁衍新類別的實例;寫入它;和 會顯示路徑。 請注意,腳本只要刪除 類別,即可從存放庫刪除 類別及其實例。

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

規格需求

需求
最低支援的用戶端
Windows Vista
最低支援的伺服器
Windows Server 2008
標頭
Wbemdisp.h
類型程式庫
Wbemdisp.tlb
DLL
Wbemdisp.dll
CLSID
CLSID_SWbemObject
IID
IID_ISWbemObject