no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | database:mssql:get_serial [2019/12/12 11:14] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Get MSSQL serial ====== | ||
+ | <code powershell> | ||
+ | Function Get-SqlServerKeys { | ||
+ | <# | ||
+ | .SYNOPSIS | ||
+ | Gets SQL Server Product Keys from local and remote SQL Servers. Works with SQL Server 2005-2014 | ||
+ | .DESCRIPTION | ||
+ | Using a string of servers, a text file, or Central Management Server to provide a list of servers, this script will go to each server and get the product key for all installed instances. Clustered instances are supported as well. Requires regular user access to the SQL instances, SMO installed locally, Remote Registry enabled and acessible by the account running the script. | ||
+ | |||
+ | Uses key decoder by Jakob Bindslet (http:// | ||
+ | |||
+ | .PARAMETER Servers | ||
+ | A comma separated list of servers. This can be the NetBIOS name, IP, or SQL instance name | ||
+ | |||
+ | .PARAMETER CentralMgmtServer | ||
+ | Compiles list of servers to inventory using all servers stored within a Central Management Server. Requires having SQL Management Studio installed. | ||
+ | |||
+ | .PARAMETER ServersFromFile | ||
+ | Uses a text file as input. The file must be formatted as such: | ||
+ | sqlserver1 | ||
+ | sqlserver2 | ||
+ | |||
+ | .NOTES | ||
+ | Author | ||
+ | Requires: PowerShell Version 3.0, SQL Server SMO, Remote Registry | ||
+ | Version: 0.8.3 | ||
+ | DateUpdated: | ||
+ | |||
+ | .LINK | ||
+ | https:// | ||
+ | |||
+ | .EXAMPLE | ||
+ | Get-SqlServerKeys winxp, sqlservera, sqlserver2014a, | ||
+ | Gets SQL Server versions, editions and product keys for all instances within each server or workstation. | ||
+ | |||
+ | .EXAMPLE | ||
+ | Get-SqlServerKeys -CentralMgmtServer sqlserver01 | ||
+ | Gets SQL Server versions, editions and product keys for all instances within sqlserver01' | ||
+ | |||
+ | .EXAMPLE | ||
+ | Get-SqlServerKeys -ServersFromFile C: | ||
+ | Gets SQL Server versions, editions and product keys for all instances listed within C: | ||
+ | #> | ||
+ | #Requires -Version 3.0 | ||
+ | [CmdletBinding(DefaultParameterSetName=" | ||
+ | |||
+ | Param( | ||
+ | [parameter(Position=0)] | ||
+ | [string[]]$Servers, | ||
+ | # Central Management Server | ||
+ | [string]$CentralMgmtServer, | ||
+ | # File with one server per line | ||
+ | [string]$ServersFromFile | ||
+ | ) | ||
+ | |||
+ | BEGIN { | ||
+ | |||
+ | Function Unlock-SQLServerKey { | ||
+ | param( | ||
+ | [Parameter(Mandatory = $true)] | ||
+ | [byte[]]$data, | ||
+ | [int]$version | ||
+ | ) | ||
+ | try { | ||
+ | if ($version -ge 11) { $binArray = ($data)[0..66] } else { $binArray = ($data)[52..66] } | ||
+ | $charsArray = " | ||
+ | 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]:: | ||
+ | $k = $k % 24 | ||
+ | } | ||
+ | $productKey = $charsArray[$k] + $productKey | ||
+ | if (($i % 5 -eq 0) -and ($i -ne 0)) { | ||
+ | $productKey = " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | catch { $productkey = " | ||
+ | return $productKey | ||
+ | } | ||
+ | } | ||
+ | |||
+ | PROCESS { | ||
+ | if ((Get-Host).Version.Major -lt 3) { throw " | ||
+ | |||
+ | if ([Reflection.Assembly]:: | ||
+ | { throw " | ||
+ | |||
+ | if ($CentralMgmtServer) { | ||
+ | if ([Reflection.Assembly]:: | ||
+ | { throw " | ||
+ | |||
+ | $server = New-Object Microsoft.SqlServer.Management.Smo.Server $CentralMgmtServer | ||
+ | $sqlconnection = $server.ConnectionContext.SqlConnectionObject | ||
+ | |||
+ | try { $cmstore = new-object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($sqlconnection)} | ||
+ | catch { throw " | ||
+ | $dbstore = $cmstore.DatabaseEngineServerGroup | ||
+ | $servers = $dbstore.GetDescendantRegisteredServers().servername | ||
+ | # Add the CM server itself, which can't be stored in the CM server. | ||
+ | $servers += $CentralMgmtServer | ||
+ | $basenames = @() | ||
+ | foreach ($server in $servers) { $basenames += $server.Split(" | ||
+ | $servers = $basenames | Get-Unique | ||
+ | } | ||
+ | |||
+ | If ($ServersFromFile) { | ||
+ | if ((Test-Path $ServersFromFile) -eq $false) { throw "Could not find file: $ServersFromFile" | ||
+ | $servers = Get-Content $ServersFromFile | ||
+ | } | ||
+ | |||
+ | if ([string]:: | ||
+ | |||
+ | $basepath = " | ||
+ | # Loop through each server | ||
+ | $objectCollection = @() | ||
+ | foreach ($servername in $servers) { | ||
+ | $servername = $servername.Split(" | ||
+ | |||
+ | if ($servername -eq " | ||
+ | $localmachine = [Microsoft.Win32.RegistryHive]:: | ||
+ | $defaultview = [Microsoft.Win32.RegistryView]:: | ||
+ | $reg = [Microsoft.Win32.RegistryKey]:: | ||
+ | } else { | ||
+ | # Get IP for remote registry access. It's the most reliable. | ||
+ | try { $ipaddr = ([System.Net.Dns]:: | ||
+ | catch { Write-Warning " | ||
+ | |||
+ | try { | ||
+ | $reg = [Microsoft.Win32.RegistryKey]:: | ||
+ | } catch { Write-Warning " | ||
+ | } | ||
+ | |||
+ | $instances = $reg.OpenSubKey(" | ||
+ | if ($instances -eq $null) { Write-Warning "No instances found on $servername. Moving on."; continue } | ||
+ | # Get Product Keys for all instances on the server. | ||
+ | foreach ($instance in $instances.GetValueNames()) { | ||
+ | if ($instance -eq " | ||
+ | |||
+ | $subkeys = $reg.OpenSubKey(" | ||
+ | $instancekey = $subkeys.GetSubKeynames() | Where-Object { $_ -like " | ||
+ | if ($instancekey -eq $null) { $instancekey = $instance } # SQL 2k5 | ||
+ | |||
+ | # Cluster instance hostnames are required for SMO connection | ||
+ | $cluster = $reg.OpenSubKey(" | ||
+ | if ($cluster -ne $null) { | ||
+ | $clustername = $cluster.GetValue(" | ||
+ | if ($instance -eq " | ||
+ | } | ||
+ | |||
+ | Write-Verbose " | ||
+ | $server = New-Object Microsoft.SqlServer.Management.Smo.Server $sqlserver | ||
+ | try { $server.ConnectionContext.Connect() } catch { Write-Warning " | ||
+ | $servicePack = $server.ProductLevel | ||
+ | |||
+ | switch ($server.VersionMajor) { | ||
+ | 9 { | ||
+ | $sqlversion = "SQL Server 2005 $servicePack" | ||
+ | $findkeys = $reg.OpenSubKey(" | ||
+ | foreach ($findkey in $findkeys.GetValueNames()) { | ||
+ | if ($findkey -like " | ||
+ | } | ||
+ | } | ||
+ | 10 { | ||
+ | $sqlversion = "SQL Server 2008 $servicePack" | ||
+ | $key = " | ||
+ | if ($server.VersionMinor -eq 50) { $key += " | ||
+ | $key += " | ||
+ | } | ||
+ | 11 { $key = " | ||
+ | 12 { $key = " | ||
+ | default { Write-Warning "SQL version not currently supported."; | ||
+ | } | ||
+ | if ($server.Edition -notlike " | ||
+ | try { | ||
+ | $subkey = Split-Path $key; $binaryvalue = Split-Path $key -leaf | ||
+ | $binarykey = $($reg.OpenSubKey($subkey)).GetValue($binaryvalue) | ||
+ | } catch {$sqlkey = "Could not connect." | ||
+ | $sqlkey = Unlock-SQLServerKey $binarykey $server.VersionMajor | ||
+ | } else { $sqlkey = "SQL Server Express Edition" | ||
+ | $server.ConnectionContext.Disconnect() | ||
+ | |||
+ | $object = New-Object PSObject -Property @{ | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | $objectCollection += $object | ||
+ | } | ||
+ | $reg.Close() | ||
+ | } | ||
+ | $objectCollection | Select "SQL Instance", | ||
+ | } | ||
+ | |||
+ | END { | ||
+ | # | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ |