Windows 7 如何从PowerShell中的InstallProductKey(SoftwareLicensingService)获取错误原因?

Windows 7 如何从PowerShell中的InstallProductKey(SoftwareLicensingService)获取错误原因?,windows-7,com,powershell,wmi,Windows 7,Com,Powershell,Wmi,我正在使用一些PowerShell来配置Windows产品密钥和激活。我得到一个and调用的实例,如下所示。带有超级格式的陷阱块额外用于帮助调试 trap [Exception] { "==================================================" "Trapped: $($Error[0])" "==================================================" "Exception

我正在使用一些PowerShell来配置Windows产品密钥和激活。我得到一个and调用的实例,如下所示。带有超级格式的
陷阱
块额外用于帮助调试

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
错误条件是无效的产品密钥。我知道这一点,因为我从系统面板手动将其输入到激活Windows对话框中。但是,该脚本仅向我显示
WMIMethodException
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
==================================================
捕获:调用“InstallProductKey”时出现异常:“”
==================================================
异常:System.Runtime.InteropServices.COMException(0xC004F025)
在System.Runtime.InteropServices.Marshal.ThroweExceptionForhr内部(Int32 errorCode,IntPtr errorInfo)
位于System.Management.ManagementObject.InvokeMethod(字符串方法名、ManagementBaseObject输入参数、InvokeMethodOptions)
位于System.Management.Automation.ManagementObjectAdapter.InvokeManagementMethod(ManagementObject对象,字符串方法名,ManagementBaseObject输入参数)
--------------------------------------------------
调用“InstallProductKey”时发生异常:“”
第14行字符:31

+$service.InstallProductKey据我所知,那里没有消息。将这些添加到陷阱中:

$_ | fl * -Force
$_.Exception | fl * -Force
返回异常中的所有内容,但没有任何有用的内容。所以我在谷歌上搜索了一下,在这里找到了一段C代码:他们在处理管理异常,而在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)”

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

$_ | fl * -Force
$_.Exception | fl * -Force
返回异常中的所有内容,但没有任何有用的内容。所以我在谷歌上搜索了一下,在这里找到了一段C代码:他们在处理管理异常,而在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)”