A while back I wrote a small script to retrieve the Windows license key. Since then I've been asked several times how to retrieve the product key of an SQL Server.
So today I threw together this small script to do just that
function Get-SQLserverKey {
## function to retrieve the license key of a SQL 2008 Server.
## by Jakob Bindslet (jakob@bindslet.dk)
param ($targets = ".")
$hklm = 2147483650
$regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup"
$regValue1 = "DigitalProductId"
$regValue2 = "PatchLevel"
$regValue3 = "Edition"
Foreach ($target in $targets) {
$productKey = $null
$win32os = $null
$wmi = [WMIClass]"\\$target\root\default:stdRegProv"
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue1)
[string]$SQLver = $wmi.GetstringValue($hklm,$regPath,$regValue2).svalue
[string]$SQLedition = $wmi.GetstringValue($hklm,$regPath,$regValue3).svalue
$binArray = ($data.uValue)[52..66]
$charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
## decrypt base24 encoded binary data
For ($i = 24; $i -ge 0; $i--) {
$k = 0
For ($j = 14; $j -ge 0; $j--) {
$k = $k * 256 -bxor $binArray[$j]
$binArray[$j] = [math]::truncate($k / 24)
$k = $k % 24
}
$productKey = $charsArray[$k] + $productKey
If (($i % 5 -eq 0) -and ($i -ne 0)) {
$productKey = "-" + $productKey
}
}
$win32os = Get-WmiObject Win32_OperatingSystem -computer $target
$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty OSCaption -value $win32os.Caption
$obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
$obj | Add-Member Noteproperty SQLver -value $SQLver
$obj | Add-Member Noteproperty SQLedition -value $SQLedition
$obj | Add-Member Noteproperty ProductKey -value $productkey
$obj
}
}
Use the function to retrieve the Product Key from the local PC:
Get-SQLserverKey
Or to retrieve the Product Key from one or more PCs (locally or remotely):
Get-SQLserverKey "pc1", "pc2", "server999", "server777"
Remeber that the output from the function is a standard PowerShell object, so you can pipe into sort-object, format-table or mayby ConvertTo-HTML ...
Thursday, November 11, 2010
Subscribe to:
Post Comments (Atom)



23 comments:
The above script works against SQL Server 2008 and SQL Server 2008 R2, NOT SQL 2005.
Thanks very much! it helped me.
Partha
This doesn't seem to work for me??? Although I am only a powershell novice. I have tried to run this directly in Powershell, and from the run prompt, but nothing is displayed.
Would you mind posting details on how exactly to run this??
Copy the function to your PowerShell prompt.
Use the function like this:
Get-SQLServerKey
Please note that it is the servername, not server\instance.
If the script is used remotely (ie. not on the server running SQL Server), sufficient rights on the windows box, need to be in order.
Worked like a charm.
Copy the code into the powershell window.
Very cool thanks!!
Hi Jakob. I'm running the script, but I'm getting no output. The server is 64 bit, does that make a difference?
Thanks for the script. I have the same issue as Lars. Server is 64 bit and I'm getting no output. I'm very much a newbie to Powershell - actually this is the first thing I've tried to run.
Even I tried on server, 64 bit Enterprise Edition but ran into errors...
I am running on SQL 2008 64bit and do not receive any output. Has there been any updates for the script?
I've received quite a lot of questions regarding this script.
Please note that:
To use the script you should start PowerShell, copy'n'paste the function into the session (press ENTER).
THEN you can use the function by typing:
Get-SQLserverKey
This should procude output similar to the following:
Computer : MyServer001.domain.com
OSCaption : Microsoft Windows Server 2008 R2 Enterprise
OSArch : 64-bit
SQLver : 10.50.1600.1
SQLedition : Enterprise Edition
ProductKey : AAAAA-BBBBB-CCCCC-DDDD-EEEEE
The function has been testet on both 32- and 64-bit SQL Servers.
Hi Jakob,
many thanks for your nice skript. Works like a charm.
I'd be interested in the inverse function too: writing a new PID to the registry. Dom you have an idea?
Cheers,
Chr!s from Hamburg, Germany
Your script works great! Saved my day today. Thanks a bunch and keep up the good work :-)
-Arun.
Jakob,
thanks heaps for sharing, works for me!
Good day! And quite by chance, you do not have a procedure that would have replaced the key installation? The problem is that when you install the correct key is not used. Now the license purchased, the correct key is, but peretavlyat SQL server is not desirable. I have installed MS SQL 2008 R2 Standart Edition. Thank you in advance for your help!
How to change the product key for SQL 2008 R2, I need to do this for 100+ computers, pls help me on this
Is it possible to know with a powershell script how many CALs are in the SQL server?
Thanks
Thanks for this Jacob. Once I ran it int the correct Powershell environment, it worked first try.
This was exceedingly usefuly for a customer that had SQL installed by my IT predecessor and lost the licensing key.
The script worked for me. Many thanks to the author!
I had to reroute the script to look for the DigitalProductID in a different location, but it worked like a charm!!! Thanks....
Thanks a lot!
i cannot get past this code: $binArray = ($data.uValue)[52..66]
Cannot index into a null array.
At line:17 char:36
+ $binArray = ($data.uValue)[ <<<< 52..66]
+ CategoryInfo : InvalidOperation: (System.Object[]:Object[]) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
i copied and pasted from the article.
awesome..works like a charm..
thanks a lot
Post a Comment