WMIMethodException with .InstallProductKey

3.8k views Asked by At

First off, this is my first post, so if I incorrectly posted this in the wrong location, please let me know.

So, what we're trying to accomplish is building a powershell script that we can throw on our workstation image so that once our Windows 10 boxes are done imaging, that we can click on a powershell script, have it pull the key from the BIOS, and automagically activate it. That being said, here is the script that we've put together from various sources.


(Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey | out-file c:\license.txt

$computer = gc env:computername

$key = get-content c:\license.txt

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

$service.InstallProductKey($key)        <--------THIS IS WHERE IT FAILS

$service.RefreshLicenseStatus()

We start running into the issues on the line $service.InstallProductKey($key). It seems, that no matter how we try to invoke that, it will consistently fail with the error "Exception calling "InstallProductKey"". I've even replaced the variable ($key) with the specific activation key, and it STILL fails with the same error.

The reason we have it outputting to a license txt file part way through is so that we can verify that the command is indeed pulling the product key (which it is).

At this point, I'm not sure where to go. It seems that people have tried to do this before, however, nobody has really wrapped up their posting with what worked and/or what didn't. I can't imagine that this is impossible, but I'm also not fond of wasting anymore time than needed, so anybody that has any insight into this issue, I'd be very grateful.

We've gotten it to work on two machines that were previously activated, and later deactivated, but on new machines that have been freshly imaged, and have yet to be activated, it will fail every time.

2

There are 2 answers

0
Ranadip Dutta On

Two things as per my observation:

(Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey | out-file c:\license.txt

I don't think that it is returning any value to your license.txt. If yes, then I would like you to see if there is any space before and after the license key. You can use trim during getting the content from the file.

Second thing, when you are getting the content from the file make sure it is not separating into multiple lines. In that case, you have to cast it as string like [String]$key or you can call toString() method for this.

One more important thing is to refresh after the installation.

$service.RefreshLicenseStatus()

Note: Make sure you are running the shell in elevated mode.

Alternative: Try Hardcoding the values and see the result

$key = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" # hardcode the key
$computer= "Computer01" # Hardcode the computer
$service = get-wmiObject -query "select * from SoftwareLicensingService" -computername $computer
$service.InstallProductKey($key)
$service.RefreshLicenseStatus()

For further thing ,please post the exact error. Hope it helps...!!!

0
elexis On

Found out that the key from Get-WmiObject has whitespace on the end. The original command will work if a .Trim() is added. Also not running as administrator will result in the same error.

(Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey | out-file c:\license.txt
$computer = gc env:computername
$key = (get-content c:\license.txt).Trim()  #trim here
$service = get-wmiObject -query “select * from SoftwareLicensingService” -computername $computer
$service.InstallProductKey($key) 
$service.RefreshLicenseStatus()