How To Retrieve the MS SQL Server Product Key Using Power Shell:A Step-by-Step Guide

preview_player
Показать описание
Join this channel to get access to perks:

Join the Telegram Group for the scripts and assistance:

You Can Also Download the scripts from below folder
We strongly believe there is always chance of betterment, so suggestions are most welcome.

Happy learning, and All the Best in your professional journey!

The journey of improvement is ongoing and never be an end.

Connect With me,

#azuresql #azure #sqldba #sqlserverdba #sql #sqlserver #sqlserverdeveloper #performance #performancetuning #performanceoptimization #mssql #mssqlserver #mssqlserverdba

Thank you!
MS SQL DBA Tech Support
Рекомендации по теме
Комментарии
Автор

Registry path
SQL

Here MSSQL15.SQL2019STANDARD should be your own ms sql server name



# Tested on Windows client versions from SQL Server 2014-2022 -->All went success.
function {
$localmachine =
$defaultview =
$reg = [Microsoft.Win32.RegistryKey]::OpenBaseKey($localmachine, $defaultview)
$key = "SOFTWARE\Microsoft\Microsoft SQL Server\$InstanceName\Setup"
$encodedData =
$reg.Close()

try {
$binArray = ($encodedData)[0..66]
$productKey = $null

$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"

$isNKey = / 0x6) -band 0x1) -ne 0;
if ($isNKey) {
$binArray[14] = $binArray[14] -band 0xF7
}

$last = 0

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
$last = $k
}

if ($isNKey) {
$part1 = $productKey.Substring(1, $last)
$part2 = $productKey.Substring(1, $productKey.Length-1)
if ($last -eq 0) {
$productKey = "N" + $part2
}
else {
$productKey = + $part1.Length, "N")
}
}

$productKey = $productKey.Insert(20, "-").Insert(15, "-").Insert(10, "-").Insert(5, "-")
}
catch {
$productkey = "Cannot decode product key."
}

$productKey
}


--Replace with your ms sql server name








MSSQLDBATechSupport