开发者

Question about the WMI Win32_WindowsProductActivation class and the SetProductKey method

开发者 https://www.devze.com 2023-01-24 03:07 出处:网络
i have a question about the Win32_WindowsProductActivation WMI class and theSetProductKey method. when i run this code (vbscript) generated with the WMi Code creator, the execution fails with the err

i have a question about the Win32_WindowsProductActivation WMI class and the SetProductKey method.

when i run this code (vbscript) generated with the WMi Code creator, the execution fails with the error 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 = objSh开发者_运维百科are.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

but if i use this code works ok, using the InstancesOf method.

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

the quiestions is

why the first code fails ? or why this wmi class requires execute this method using the InstancesOf?


you must call and pass directly the parameters of SetProductKey method without use the SpawnInstance_ because this method is non-static.

the rule is, if the wmi method to execute is static you can use theSpawnInstance_ otherwise call the method passing directly the parameters

Here you have a description of static and non.static methods.

Static methods apply only to WMI classes and not to specific instances of a class. For example, the Create method of the Win32_Process class is a static method because use it to create a new process without an instance of this class. Nonstatic methods apply only to instances of a class. For example, the Terminate method of the Win32_Process class is a nonstatic method because it only makes sense to terminate a process if an instance of that process exists. You can determine if a method is static by checking if the Static qualifier is associated with the method.

Additionally you can check this article Calling a Provider Method

0

精彩评论

暂无评论...
验证码 换一张
取 消