How do I get an error reason from InstallProductKey (SoftwareLicensingService) in PowerShell?

前端 未结 1 348
醉酒成梦
醉酒成梦 2021-01-25 00:06

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. T

相关标签:
1条回答
  • 2021-01-25 01:08

    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)"

    0 讨论(0)
提交回复
热议问题