This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Get-WindowsProductKey.ps1
156 lines (130 loc) · 4.97 KB
/
Get-WindowsProductKey.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Get-WindowsProductKey.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Get the Windows product key and some usefull informations about the system
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Get the Windows product key and some usefull informations about the system
.DESCRIPTION
Get the Windows product key from a local or remote system and some informations like Serialnumber, Windows version, Bit-Version etc. from one or more computers. Remote computers need WinRM enabled. To do this use "winrm quickconfig".
Basic Logic found on: http://powershell.com/cs/blogs/tips/archive/2012/04/30/getting-windows-product-key.aspx
.EXAMPLE
Get-WindowsProductKey
ComputerName : TEST-PC-01
WindowsVersion : Microsoft Windows 10 Pro
CSDVersion :
BitVersion : 64-bit
BuildNumber : 10586
ProductID : 00000-00000-00000-00000
ProductKey : XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
.EXAMPLE
Get-WindowsProductKey -ComputerName TEST-PC-01,TEST-PC-02
ComputerName : TEST-PC-01
WindowsVersion : Microsoft Windows 10 Pro
CSDVersion :
BitVersion : 64-bit
BuildNumber : 10586
ProductID : 00000-00000-00000-00000
ProductKey : XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
ComputerName : TEST-PC-02
WindowsVersion : Microsoft Windows 10 Pro
CSDVersion :
BitVersion : 64-bit
BuildNumber : 10586
ProductID : 00000-00000-00000-00000
ProductKey : XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Get-WindowsProductKey.README.md
#>
function Get-WindowsProductKey
{
[CmdletBinding()]
param(
[Parameter(
Position=0,
HelpMessage='ComputerName or IPv4-Address of the remote computer')]
[String[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(
Position=1,
HelpMessage='Credentials to authenticate agains a remote computer')]
[System.Management.Automation.PSCredential]
[System.Management.Automation.CredentialAttribute()]
$Credential
)
Begin{
$LocalAddress = @("127.0.0.1","localhost",".","$($env:COMPUTERNAME)")
[System.Management.Automation.ScriptBlock]$Scriptblock = {
$ProductKeyValue = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").digitalproductid[0x34..0x42]
$Wmi_Win32OperatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Caption, CSDVersion, Version, OSArchitecture, BuildNumber, SerialNumber
[pscustomobject] @{
ProductKeyValue = $ProductKeyValue
Wmi_Win32OperatingSystem = $Wmi_Win32OperatingSystem
}
}
}
Process{
foreach($ComputerName2 in $ComputerName)
{
$Chars="BCDFGHJKMPQRTVWXY2346789"
# Don't use Invoke-Command on local machine. Prevent errors if WinRM is not configured
if($LocalAddress -contains $ComputerName2)
{
$ComputerName2 = $env:COMPUTERNAME
$Scriptblock_Result = Invoke-Command -ScriptBlock $Scriptblock
}
else
{
if(-not(Test-Connection -ComputerName $ComputerName2 -Count 2 -Quiet))
{
Write-Error -Message "$ComputerName2 is not reachable via ICMP!" -Category ConnectionError
continue
}
try {
if($PSBoundParameters['Credential'] -is [System.Management.Automation.PSCredential])
{
$Scriptblock_Result = Invoke-Command -ScriptBlock $Scriptblock -ComputerName $ComputerName2 -Credential $Credential -ErrorAction Stop
}
else
{
$Scriptblock_Result = Invoke-Command -ScriptBlock $Scriptblock -ComputerName $ComputerName2 -ErrorAction Stop
}
}
catch {
Write-Error -Message "$($_.Exception.Message)" -Category ConnectionError
continue
}
}
$ProductKey = ""
for($i = 24; $i -ge 0; $i--)
{
$r = 0
for($j = 14; $j -ge 0; $j--)
{
$r = ($r * 256) -bxor $Scriptblock_Result.ProductKeyValue[$j]
$Scriptblock_Result.ProductKeyValue[$j] = [math]::Floor([double]($r/24))
$r = $r % 24
}
$ProductKey = $Chars[$r] + $ProductKey
if (($i % 5) -eq 0 -and $i -ne 0)
{
$ProductKey = "-" + $ProductKey
}
}
[pscustomobject] @{
ComputerName = $ComputerName2
Caption = $Scriptblock_Result.Wmi_Win32OperatingSystem.Caption
CSDVersion = $Scriptblock_Result.Wmi_Win32OperatingSystem.CSDVersion
WindowsVersion = $Scriptblock_Result.Wmi_Win32OperatingSystem.Version
OSArchitecture = $Scriptblock_Result.Wmi_Win32OperatingSystem.OSArchitecture
BuildNumber = $Scriptblock_Result.Wmi_Win32OperatingSystem.BuildNumber
SerialNumber = $Scriptblock_Result.Wmi_Win32OperatingSystem.SerialNumber
ProductKey = $ProductKey
}
}
}
End{
}
}