加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

vbscript – 关于WMI Win32_WindowsProductActivation类和SetPro

发布时间:2020-12-14 05:39:28 所属栏目:Windows 来源:网络整理
导读:我有一个关于 Win32_WindowsProductActivation WMI类和SetProductKey方法的问题. 当我运行使用WMi Code创建者生成的此代码(vbscript)时,执行失败并显示错误Invalid参数 strComputer = "." Set objWMIService = GetObject("winmgmts:" strComputer "rootC
我有一个关于 Win32_WindowsProductActivation WMI类和SetProductKey方法的问题.

当我运行使用WMi Code创建者生成的此代码(vbscript)时,执行失败并显示错误Invalid参数

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootCIMV2") 
' 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

静悄悄是

为什么第一个代码失败?或者为什么这个wmi类需要使用InstancesOf执行此方法?

解决方法

你必须调用并直接传递SetProductKey方法的参数而不使用SpawnInstance_,因为这种方法是非静态的.

规则是,如果执行的wmi方法是静态的,你可以使用theSpawnInstance_否则调用直接传递参数的方法

这里有静态和非静态方法的描述.

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.

另外,您可以查看这篇文章Calling a Provider Method

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读