SWbemServices.Delete 方法

SWbemServices物件的Delete方法會刪除物件路徑中指定的類別或實例。 您只能刪除目前命名空間中的物件。

如果動態提供者提供類別或實例,除非提供者支援類別或實例刪除,否則您無法刪除此物件。

這個方法會在同步模式中呼叫。 如需詳細資訊,請參閱 呼叫方法

如需此語法的說明,請參閱 腳本 API 的檔慣例

語法

SWbemServices.Delete( _
  ByVal strObjectPath, _
  [ ByVal iFlags ], _
  [ ByVal objWbemNamedValueSet ] _
)

參數

strObjectPath

必要。 字串,包含您要刪除之物件的物件路徑。 如需詳細資訊,請參閱 描述 WMI 物件的位置

iFlags [選擇性]

保留的。 這個值必須為零。

objWbemNamedValueSet [選擇性]

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

傳回值

這個方法不會傳回值。

錯誤碼

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

wbemErrAccessDenied - 2147749891 (0x80041003)

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

wbemErrFailed - 2147749889 (0x80041001)

未指定的錯誤。

wbemErrInvalidClass - 2147749904 (0x80041010)

指定的類別不存在。

wbemErrInvalidOperation - 2147749910 (0x80041016)

無法刪除物件。

wbemErrNotFound - 2147749890 (0x80041002)

物件不存在。

wbemErrOutOfMemory - 2147749894 (0x80041006)

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

備註

當物件的索引鍵屬性未指定值時,可以使用 SWbemServices.Delete 方法,因為此方法只需要物件路徑做為輸入。 這個方法可用於 SWbemObject.Delete_因為缺少 索引鍵值而失敗的情況。 如果物件透過 SWbemObject.Put_認可至 WMI,則會透過呼叫取得 SWbemObjectPath 物件。

範例

下列範例會建立新的類別、新增不是索引鍵的屬性、將新類別寫入存放庫,並顯示新類別物件的路徑。 然後,腳本會繁衍新類別的實例、寫入 實例,並顯示路徑。 請注意,腳本會藉由刪除 類別,從存放庫刪除 類別及其實例。 如需 WMI 類別和實例的詳細資訊,請參閱操作類別和實例資訊和一般資訊模型

wbemCimtypeSint32 = 3
Set objSWbemService = GetObject("Winmgmts:root\default")
Set objClass = objSWbemService.Get()
objClass.Path_.Class = "NewClass"

' Add a property
' Integer property
objClass.Properties_.Add "iProperty", wbemCimtypeSint32  
objClass.Properties_("iProperty").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.iProperty = 1000

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

' Remove the new class and instance from the repository
objSWbemService.Delete("NewClass")
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_SWbemServices
IID
IID_ISWbemServices

另請參閱

SWbemServices

SWbemObjectPath