SQL server 2012 数据库 序列号查看

一、序列号保存在哪

--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这个键。

二、如何解码序列号

利用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序列号里字符的格式发生了变化, b i n A r r a y = ( binArray = ( binArray=(data.uValue)[0…16] 不同于SQL Server 2008的 b i n A r r a y = ( binArray = ( 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
}
}

调用powershell函数并输出序列号
打开powershell,把上面的函数贴进去,回车,输入Get-SQLServerKey 并回车;

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

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

输出结果如下:
1589421254(1).jpg

根据powershell 脚本翻译成的Python base24 解码函数:

import math
# x是16进制数
def base24encode(x):
    int_list = []
    str1 = str(bin(x)).replace('0b', '')
    for b in range(0, int(len(str1) / 8)):
        int_list.append(int(str1[(b * 8):(b * 8 + 8)], 2))
    print(int_list)

    i = 24
    productKey = ''
    sel = "BCDFGHJKMPQRTVWXY2346789"
    while i >= 0:
        k = 0
        j = 14
        while j>= 0:
            k = (k *256) ^ int(int_list[j])
            int_list[j] = math.modf(k / 24)[1]
            k = k % 24
            j = j - 1
        productKey = sel[k] + productKey
        i= i-1
    return productKey

 
x = 0x85443B934966ADCB433B3A1234264212
print(base24encode(x))

猜你喜欢

转载自blog.csdn.net/sinat_35773915/article/details/131942953