如何从PowerShell中的InstallProductKey(SoftwareLicensingService)获取错误原因? [英] How do I get an error reason from InstallProductKey (SoftwareLicensingService) in PowerShell?

查看:769
本文介绍了如何从PowerShell中的InstallProductKey(SoftwareLicensingService)获取错误原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些PowerShell 功能以配置Windows产品密钥和激活。我收到了一个 SoftwareLicensingService ,并致电 InstallProductKey ,像这样。 陷阱阻止超级格式是额外帮助调试。

 陷阱[例外] 
{
==================== =============
陷阱:$($ Error [0])
=============== =================================
异常:$($ _。异常)
-------------------------------------------- ------

break
}

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

错误条件是无效的产品密钥。我知道这是因为我手动将它输入到系统面板中的激活Windows 对话框中。但是,脚本只显示了 WMIMethodException COMException

  ================================== ============== 
Trapped:异常调用InstallProductKey:
================== ==============================
例外:System.Runtime.InteropServices.COMException(0xC004F025)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo)
在System.Management.ManagementObject.InvokeMethod(String methodName,ManagementBaseObject inParameters,InvokeMethodOptions选项)
在System.Management。 Automation.ManagementObjectAdapter.InvokeManagementMethod(ManagementObject obj,String methodName,ManagementBaseObject inParams)
------------------------------- -------------------

异常调用InstallProductKey:
在行:14 char:31
+ $ service.InstallProductKey<<<< (12345-12345-12345-12345-12345)
+ CategoryInfo:NotSpecified:(:) [],ParentContainsErrorRecordException
+ FullyQualifiedErrorId:WMIMethodException
pre>

我没有从方法中获得返回代码(尽管文档说明了我也没有找到错误代码列表)。 您知道如何获得激活(或产品密钥安装)错误原因吗?

解决方案

就像我看到的那里没有消息。将这些添加到您的陷阱中:

  $ _ fl * -Force 
$ _。异常| fl * -Force

返回异常中的所有内容,没有什么用处。所以我google了一下,发现一个C#代码在这里: http://www.dozty.com/?tag=change-windows-7-product-key-c-sharp 他们是cathcing 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)


I'm using some PowerShell functions to configure Windows product keys and activation. I get an instance of the SoftwareLicensingService and call InstallProductKey, like this. The trap block with super formatting is extra to help debugging.

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

The error condition is an invalid product key. I know this because I entered it manually into the Activate Windows dialog from the System panel. But, the script only ever shows me the WMIMethodException or the COMException.

==================================================
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

I don't get a return code from the method (despite the documentation stating I do, can't find a list of error codes anyway). Do you know how to get the activation (or product key install) error reason?

解决方案

As far as I can see there's no message there. Adding these to your trap:

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

Returns everything that is there in the exception and there is nothing useful. So I googled a bit and found a piece of C# code here: http://www.dozty.com/?tag=change-windows-7-product-key-c-sharp They were cathcing ManagementException and in C# it seemed to work a bit better. I have rewritten that code into PowerShell and was trying to catch ManagementException, but without luck:

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)

It throws: Cannot convert the "System.Runtime.InteropServices.COMException (0xC004F050)"

这篇关于如何从PowerShell中的InstallProductKey(SoftwareLicensingService)获取错误原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆