當需要再次安裝SQL Server時,如果序號找不到了,可以試著從已經安裝的執行個體裡找回序號,因為安裝完SQL Server後,序號(Product Key)被儲存在註冊表裡;
MSDN訂閱下載的安裝包是內建序號的,則沒有這個困擾。
一. 序號儲存在哪
通過無文檔記載的擴充預存程序xp_regread讀取註冊表:
--For SQL Server 2008, 2008 R2use masterGOexec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup','ProductCode'exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup','DigitalProductID'GO--For SQL Server 2012use masterGOexec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup','ProductCode'exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup','DigitalProductId'GO
不要被ProductCode迷惑,就算只安裝了SQL Server用戶端,註冊表裡也會有這個索引值,並不是序號,DigitalProductID才是,但經過了Base24編碼,需要解碼才行。
可以看到,對於不同版本,註冊表的路徑不一樣,但是鍵是一致的。
Express版是免費的,沒有序號,從而註冊表也沒DigitalProductID這個鍵。
二. 如何解碼序號
1. Base24, Base 64 編碼簡介
可能很多人聽說過Base64編碼,它用於對比較長的字串進行編碼,以方便傳輸;
Base24編碼主要應用在序號產生上,兩者的實現思路是類似的,只是編碼的模式有點變化。
Base64所對應的編碼錶是:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
共計64個字元。
Base24所對應的編碼錶是:
BCDFGHJKMPQRTVWXY2346789
共計24個字元,這裡主要去掉了一些對於序號來說不容易識別和容易混淆的字元。
SQL Server的序號採用的是Base24編碼,對編碼後的字元進行解碼,即可以得到原文。
編碼/解碼並不是加密/解密,沒有秘鑰的說法,只有字元轉換的規則,Base24, Base64詳細的演算法可以在網上找到。
2. 利用Powershell 解碼
以下powershell函數用於解碼/找回SQL Server序號,在SQL Server 2008, 2008 R2執行個體上測試通過:
function Get-SQLServerKey { ## function to retrieve the license key of a SQL 2008 Server. 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 }}
SQL Server 2012序號裡字元的格式發生了變化,$binArray = ($data.uValue)[0..16] 不同於SQL Server 2008的$binArray = ($data.uValue)[52..66],同時別忘了改下註冊表路徑$regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup",修改後如下,在SQL Server 2012執行個體上測試通過:
function Get-SQLServerKey {## function to retrieve the license key of a SQL 2012 Server.## by Jakob Bindslet (jakob@bindslet.dk)## 2012 Modification by Xian Wang (daanno2@gmail.com)param ($targets = ".")$hklm = 2147483650$regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\110\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)[0..16]$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 dataFor ($i = 24; $i -ge 0; $i--) {$k = 0For ($j = 14; $j -ge 0; $j--) {$k = $k * 256 -bxor $binArray[$j]$binArray[$j] = [math]::truncate($k / 24)$k = $k % 24}$productKey = $charsArray[$k] + $productKeyIf (($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}}
3. 調用powershell函數並輸出序號
開啟powershell,把上面的函數貼進去,斷行符號,輸入Get-SQLServerKey 並斷行符號;
或者把上面的函數存為.ps1檔案直接引用:
PS C:\Windows\system32> . C:\Users\username\Desktop\pk.ps1PS C:\Windows\system32> Get-SQLserverKey
輸出結果如下,首尾幾個字元被人為改成星號,沒有貼出來。
Computer : .OSCaption : Microsoft Windows Server 2012 R2 StandardOSArch : 64-bitSQLver : 11.2.5058.0SQLedition : Developer EditionProductKey : *****-G8T4R-QW4XX-BVH62-*****
對於SQL Server 2000, 2005,由於手頭沒有環境,沒有測試;對於Office,Windows系統,應該也都可以通過類似的方式拿到序號。
以上內容就是關於安裝完成後如何找回SQL Server執行個體安裝時的序號,希望大家有所協助。