2010-11-05 76 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