萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mssql數據庫 >> 安裝完成後如何找回SQL Server實例安裝時的序列號

安裝完成後如何找回SQL Server實例安裝時的序列號

當需要再次安裝SQL Server時,如果序列號找不到了,可以試著從已經安裝的實例裡找回序列號,因為安裝完SQL Server後,序列號(Product Key)被保存在注冊表裡;

MSDN訂閱下載的安裝包是內置序列號的,則沒有這個困擾。

一. 序列號保存在哪

通過無文檔記載的擴展存儲過程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才是,但經過了Base24編碼,需要解碼才行。

可以看到,對於不同版本,注冊表的路徑不一樣,但是鍵是一致的。

Express版是免費的,沒有序列號,從而注冊表也沒DigitalProductID這個鍵。

二. 如何解碼序列號

1. Base24, Base64 編碼簡介

可能很多人聽說過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 ([email protected])
## 2012 Modification by Xian Wang ([email protected])
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. 調用powershell函數並輸出序列號

打開powershell,把上面的函數貼進去,回車,輸入Get-SQLServerKey 並回車;

或者把上面的函數存為.ps1文件直接引用:

PS C:\Windows\system32> . C:\Users\username\Desktop\pk.ps1
PS C:\Windows\system32> Get-SQLserverKey

輸出結果如下,首尾幾個字符被人為改成星號,沒有貼出來。

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,由於手頭沒有環境,沒有測試;對於Office,Windows系統,應該也都可以通過類似的方式拿到序列號。

以上內容就是關於安裝完成後如何找回SQL Server實例安裝時的序列號,希望大家有所幫助。

copyright © 萬盛學電腦網 all rights reserved