【问题标题】:Question about the WMI Win32_WindowsProductActivation class and the SetProductKey method关于 WMI Win32_WindowsProductActivation 类和 SetProductKey 方法的问题
【发布时间】:2010-11-05 02:28:10
【问题描述】:

我对@9​​87654321@ WMI 类和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

问题是

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

【问题讨论】:

    标签: vbscript wmi


    【解决方案1】:

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

    规则是,如果要执行的w​​mi方法是静态的,可以使用SpawnInstance_,否则调用直接传参的方法

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

    静态方法仅适用于 WMI 类而不是特定实例 一类的。例如,创建 Win32_Process 类的方法是 静态方法,因为使用它来创建 一个没有实例的新进程 这节课。适用非静态方法 仅适用于类的实例。为了 例如,终止方法 Win32_Process 类是非静态的 方法,因为它只有意义 如果一个实例终止一个进程 这个过程是存在的。你可以确定 如果方法是静态的,通过检查是否 静态限定符是关联的 用方法。

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

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 2011-05-05
      • 1970-01-01
      • 2011-02-19
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多