2010-11-05 31 views
4

我有一個關於Win32_WindowsProductActivation WMI類和SetProductKey方法的問題。關於WMI Win32_WindowsProductActivation類和SetProductKey方法的問題

當我運行與WMI代碼創建器產生的代碼(VBScript)的,執行失敗,錯誤Invalid parameter

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
' Obtain an instance of the the class 
' using a key property value. 
Set objShare = objWMIService.Get("Win32_WindowsProductActivation") 

' Obtain an InParameters object specific 
' to the method. 
Set objInParam = objShare.Methods_("SetProductKey"). _ 
    inParameters.SpawnInstance_() 


' Add the input parameters. 
objInParam.Properties_.Item("ProductKey") = "QW4HDDQCRGHM64M6GJRK8K83T" 

' Execute the method and obtain the return status. 
' The OutParameters object in objOutParams 
' is created by the provider. 
Set objOutParams = objWMIService.ExecMethod("Win32_WindowsProductActivation", "SetProductKey", objInParam) 

' List OutParams 
Wscript.Echo "Out Parameters: " 
Wscript.echo "ReturnValue: " & objOutParams.ReturnValue 

但如果我使用此代碼工程確定,使用InstancesOf方法。

Dim VOL_PROD_KEY 
VOL_PROD_KEY = "QW4HDDQCRGHM64M6GJRK8K83T" 

for each Obj in GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf ("win32_WindowsProductActivation") 

result = Obj.SetProductKey (VOL_PROD_KEY) 

if err <> 0 then 
WScript.Echo Err.Description, "0x" & Hex(Err.Number) 
Err.Clear 
end if 

Next 

的quiestions是

爲什麼第一個代碼失敗?或爲什麼這個wmi類需要使用InstancesOf執行這個方法?

回答

1

您必須調用並直接傳遞參數SetProductKey方法而不使用SpawnInstance_,因爲此方法是非靜態

的規則是,如果執行WMI方法是靜態的,你可以使用SpawnInstance_否則直接調用傳遞參數

這裏有靜態和non.static方法的描述方法。

靜態方法僅適用於WMI 類而不適用於某個類的特定實例 。例如,Win32_Process類的創建 方法是一個 靜態方法,因爲使用它創建 一個新進程,但沒有此類的實例 。非靜態方法僅將 應用於類的實例。例如,對於 示例, Win32_Process類的Terminate方法是非靜態的 方法,因爲它只有在 終止進程時纔有意義,如果該進程存在 的實例。如果方法是靜態的,您可以通過檢查 靜態限定符是否與方法關聯 來確定 。

另外,你可以檢查此文章Calling a Provider Method