1

I work as a Microsoft partner for their loaning and seeding process for Surface Pro 3's and 4's. We re-image hundreds of devices a day and are having a problem with digital entitlement. I need a way to pull the OEM key from the device and force activation with that key. I am trying to accomplish this through a powershell script:

$computer = gc env:computername

$key = (Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey | Out-String


$service = get-wmiObject -query “select * from SoftwareLicensingService” -computername $computer

$service.InstallProductKey($key)

$service.RefreshLicenseStatus()

I am getting the error :

Exception calling "InstallProductKey" : ""
At line:7 char:1
+ $service.InstallProductKey((Get-WmiObject -query ‘select * from Softw ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

any help would be appreciated, either with fixing this error or if there is an easier way to accomplish what I am doing. Thanks!

EDIT: Added exception trap, new error

Cannot convert the "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)" value of type    "System.Management.Automation.ErrorRecord" to type 
"System.Management.ManagementException".
At line:3 char:1
+ [System.Management.ManagementException] $_
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
  • Before you call `$service.InstallProductKey($key)` have you validated that `$key` does, in fact, contain data? Just want to ensure you've checked this, because if `$key` is empty, you'll get an error like this. Try doing a `Write-Host $key` before you call `InstallProductKey` to be sure, and let us know. – gravity Jul 08 '16 at 15:40
  • Yes, it contains the OEM key on the device – Cory Mankowski Jul 08 '16 at 15:42
  • 1
    Take a look at [this](http://stackoverflow.com/questions/8525758/how-do-i-get-an-error-reason-from-installproductkey-softwarelicensingservice-i) to see if it offers any pointers as to how to `trap` where this is failing for you, with more details. It might help to [edit](http://stackoverflow.com/posts/38270487/edit) that into your question, once you have it. – gravity Jul 08 '16 at 15:48
  • 1
    I updated and added – Cory Mankowski Jul 08 '16 at 16:05

1 Answers1

0

Try Adding .Trim() to the end of $key

I had a similar issue with my code below, which threw the same error Exception calling "InstallProductKey" : "" It turned out the $key was returning the key string + a few blank spaces after it. Credits to @elexis for picking it up. Couldn't find a solution anywhere for this.

$computer = gc env:computername
$key = (wmic path softwarelicensingservice get oa3xoriginalproductkey)[2].Trim() #<--The Trim is to remove the white space aftewards which causes an error
Write-Output $key

$service = get-wmiObject -query "select * from SoftwareLicensingService" -computername $computer
$service.InstallProductKey($key)
$service.RefreshLicenseStatus()
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51