【问题标题】:How do I get an error reason from InstallProductKey (SoftwareLicensingService) in PowerShell?如何从 PowerShell 中的 InstallProductKey (SoftwareLicensingService) 获取错误原因?
【发布时间】:2012-01-21 11:36:14
【问题描述】:

我正在使用一些 PowerShell functions 来配置 Windows 产品密钥和激活。我得到一个SoftwareLicensingService 的实例并调用InstallProductKey,就像这样。带有超级格式的trap 块有助于调试。

trap [Exception]
{
    "=================================================="
    "Trapped:   $($Error[0])"
    "=================================================="
    "Exception: $($_.Exception)"
    "--------------------------------------------------"
    ""
    break
}

$service = Get-WmiObject -Query "SELECT * FROM SoftwareLicensingService"
$service.InstallProductKey("12345-12345-12345-12345-12345")
$service.RefreshLicenseStatus() | Out-Null

错误条件是无效的产品密钥。我知道这一点是因为我从 System 面板手动将其输入到 Activate Windows 对话框中。但是,脚本只显示WMIMethodExceptionCOMException

==================================================
Trapped:   Exception calling "InstallProductKey" : ""
==================================================
Exception: System.Runtime.InteropServices.COMException (0xC004F025)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)
   at System.Management.Automation.ManagementObjectAdapter.InvokeManagementMethod(ManagementObject obj, String methodName, ManagementBaseObject inParams)
--------------------------------------------------

Exception calling "InstallProductKey" : ""
At line:14 char:31
+ $service.InstallProductKey <<<< ("12345-12345-12345-12345-12345")
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : WMIMethodException

我没有从该方法获得返回码(尽管文档说明我这样做了,但无论如何都找不到错误代码列表)。 您知道如何获取激活(或产品密钥安装)错误原因吗?

【问题讨论】:

    标签: windows-7 com powershell wmi


    【解决方案1】:

    据我所知,那里没有消息。将这些添加到您的陷阱中:

    $_ | fl * -Force
    $_.Exception | fl * -Force
    

    返回异常中的所有内容并且没有任何用处。所以我在谷歌上搜索了一下,在这里找到了一段 C# 代码:http://www.dozty.com/?tag=change-windows-7-product-key-c-sharp 他们正在接受 ManagementException,在 C# 中它似乎工作得更好一些。我已将该代码重写为 PowerShell 并试图捕获 ManagementException,但没有运气:

    trap [Exception]
    {
    [System.Management.ManagementException] $_
    break
    }
    $classInstance = new-object System.Management.ManagementObject("root\CIMV2","SoftwareLicensingService.Version=`"6.1.7600.16385`"", $null);
    $inParams = $classInstance.GetMethodParameters("InstallProductKey")
    $inParams["ProductKey"] =  "12345-12345-12345-12345-12345"
    $classInstance.InvokeMethod("InstallProductKey", $inParams, $null)
    

    它抛出:无法转换“System.Runtime.InteropServices.COMException (0xC004F050)”

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2019-04-16
      • 2012-10-26
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      相关资源
      最近更新 更多