當(dāng)需要再次安裝SQL Server時,如果序列號找不到了,可以試著從已經(jīng)安裝的實例里找回序列號,因為安裝完SQL Server后,序列號(Product Key)被保存在注冊表里;
MSDN訂閱下載的安裝包是內(nèi)置序列號的,則沒有這個困擾。
一. 序列號保存在哪
通過無文檔記載的擴展存儲過程xp_regread讀取注冊表:
--For SQL Server 2008, 2008 R2
use master
GO
exec 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 2012
use master
GO
exec 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才是,但經(jīng)過了Base24編碼,需要解碼才行。
可以看到,對于不同版本,注冊表的路徑不一樣,但是鍵是一致的。
Express版是免費的,沒有序列號,從而注冊表也沒DigitalProductID這個鍵。
二. 如何解碼序列號
1. Base24, Base64 編碼簡介
可能很多人聽說過Base64編碼,它用于對比較長的字符串進(jìn)行編碼,以方便傳輸;
Base24編碼主要應(yīng)用在序列號生成上,兩者的實現(xiàn)思路是類似的,只是編碼的模式有點變化。
Base64所對應(yīng)的編碼表是:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
共計64個字符。
Base24所對應(yīng)的編碼表是:
BCDFGHJKMPQRTVWXY2346789
共計24個字符,這里主要去掉了一些對于序列號來說不容易識別和容易混淆的字符。
SQL Server的序列號采用的是Base24編碼,對編碼后的字符進(jìn)行解碼,即可以得到原文。
編碼/解碼并不是加密/解密,沒有秘鑰的說法,只有字符轉(zhuǎn)換的規(guī)則,Base24, Base64詳細(xì)的算法可以在網(wǎng)上找到。
2. 利用Powershell 解碼
以下powershell函數(shù)用于解碼/找回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序列號里字符的格式發(fā)生了變化,$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 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
}
}
3. 調(diào)用powershell函數(shù)并輸出序列號
打開powershell,把上面的函數(shù)貼進(jìn)去,回車,輸入Get-SQLServerKey 并回車;
或者把上面的函數(shù)存為.ps1文件直接引用:
PS C:\Windows\system32> . C:\Users\username\Desktop\pk.ps1
PS C:\Windows\system32> Get-SQLserverKey
輸出結(jié)果如下,首尾幾個字符被人為改成星號,沒有貼出來。
Computer : .
OSCaption : Microsoft Windows Server 2012 R2 Standard
OSArch : 64-bit
SQLver : 11.2.5058.0
SQLedition : Developer Edition
ProductKey : *****-G8T4R-QW4XX-BVH62-*****
對于SQL Server 2000, 2005,由于手頭沒有環(huán)境,沒有測試;對于Office,Windows系統(tǒng),應(yīng)該也都可以通過類似的方式拿到序列號。
以上內(nèi)容就是關(guān)于安裝完成后如何找回SQL Server實例安裝時的序列號,希望大家有所幫助。
您可能感興趣的文章:- java生成申請單序列號的實現(xiàn)方法
- SQL Server自動生成日期加數(shù)字的序列號
- .net獲取硬件信息_CPU序列號
- C#獲得MAC地址(網(wǎng)卡序列號)的實現(xiàn)代碼
- C#利用win32 Api 修改本地系統(tǒng)時間、獲取硬盤序列號
- mysql簡單實現(xiàn)查詢結(jié)果添加序列號的方法
- PowerShell中獲取Windows系統(tǒng)序列號的腳本分享
- C#獲取U盤序列號的方法
- C#實現(xiàn)讀取指定盤符硬盤序列號的方法
- ORACLE實現(xiàn)自定義序列號生成的方法