This repository has been archived by the owner on Jul 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
get-windowsproductkey.psm1
171 lines (140 loc) · 6.04 KB
/
get-windowsproductkey.psm1
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function Get-WindowsProductKey
{
<#
.SYNOPSIS
Retrives a Windows license key.
.DESCRIPTION
Retrives a Windows license key from local and remote computers
via a WMI call to the registry along with additonal information
from the OperatingSystem WMI class.
.PARAMETER Computers
The list of computers to get the license key information for.
.INPUTS
Strings that are placed into an array to process.
.OUTPUTS
Returns an object with operating system and licensing data.
.EXAMPLE
Get-WindowsProductKey
Node : EXAMPLE1
Hostname : EXAMPLE1
Caption : Microsoft Windows 7 Enterprise
CSDVersion : Service Pack 1
OSArch : 64-bit
BuildNumber : 7601
RegisteredTo : Windows User
ProductID : 12345-678-9876543-21234
ProductKey : ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
Returns the licensing information for the local computer.
.EXAMPLE
C:\PS> Get-WindowsProductKey -Computers EXAMPLE2, EXAMPLE3
Node : EXAMPLE2
Hostname : EXAMPLE2
Caption : Microsoft Windows Server 2012 Standard
CSDVersion :
OSArch : 64-bit
BuildNumber : 9200
RegisteredTo : Windows User
ProductID : 12345-678-9876543-21234
ProductKey : ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
Node : EXAMPLE3
Hostname : EXAMPLE3
Caption : Microsoft Windows 8 Enterprise
CSDVersion :
OSArch : 64-bit
BuildNumber : 9200
RegisteredTo : Windows User
ProductID : 12345-678-9876543-21234
ProductKey : ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
Returns the licensing information for the remote computers.
WMI call access and firewall exceptions are required.
.EXAMPLE
C:\PS> Get-WindowsProductKey -Computers 10.0.26.182, fe80::c9a5:ed01:f6d4:3890%10
Node : EXAMPLE4
Hostname : EXAMPLE4
Caption : Microsoft Windows Server 2008 R2 Standard
CSDVersion : Service Pack 1
OSArch : 64-bit
BuildNumber : 7601
RegisteredTo : Windows User
ProductID : 12345-678-9876543-21234
ProductKey : ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
Node : EXAMPLE5
Hostname : EXAMPLE5
Caption : Microsoft Windows Server 2008 R2 Standard
CSDVersion : Service Pack 1
OSArch : 64-bit
BuildNumber : 7601
RegisteredTo : Windows User
ProductID : 12345-678-9876543-21234
ProductKey : ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
IPv6 and IPv4 addresses can also be used.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$false,
Position=0)]
[Alias('Computer','Server','Node')]
[String[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$false,
Position=1)]
[PSCredential]$Credential = [PSCredential]::Empty
)
foreach($computer in $ComputerName)
{
try
{
if(-not ($computer -eq $env:COMPUTERNAME))
{
$reg = Get-WmiObject -ComputerName $computer -List -Namespace "root\default" -Credential $Credential | Where-Object {$_.Name -eq "StdRegProv"}
$win32os = Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem -Credential $Credential
}
else
{
$reg = Get-WmiObject -ComputerName $computer -List -Namespace "root\default" | Where-Object {$_.Name -eq "StdRegProv"}
$win32os = Get-WmiObject -ComputerName $computer -Class Win32_OperatingSystem
}
$values = [Byte[]]($reg.GetBinaryValue(2147483650,"SOFTWARE\Microsoft\Windows NT\CurrentVersion\DefaultProductKey","DigitalProductId").UValue)
$lookup = [Char[]]("B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9")
$keyStartIndex = [Int]52
$keyEndIndex = [Int]($keyStartIndex + 15)
$decodeLength = [Int]29
$decodeStringLength = [Int]15
$decodedChars = New-Object char[] $decodeLength
$hexPid = New-Object System.Collections.ArrayList
for($i = $keyStartIndex; $i -le $keyEndIndex; $i++){[void]$hexPid.Add($values[$i])}
for($i = $decodeLength - 1; $i -ge 0; $i--)
{
if(($i + 1) % 6 -eq 0) {$decodedChars[$i] = '-'}
else
{
$digitMapIndex = [Int]0
for ($j = $decodeStringLength - 1; $j -ge 0; $j--)
{
$byteValue = [Int](($digitMapIndex * [Int]256) -bor [Byte]$hexPid[$j])
$hexPid[$j] = [Byte]([Math]::Floor($byteValue / 24))
$digitMapIndex = $byteValue % 24
$decodedChars[$i] = $lookup[$digitMapIndex]
}
}
}
$STR = ''
$decodedChars | ForEach-Object { $str+=$_ }
$object = New-Object Object
$object | Add-Member Noteproperty Node -value $computer
$object | Add-Member Noteproperty Hostname -value $win32os.CSName
$object | Add-Member Noteproperty Caption -value $win32os.Caption
$object | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
$object | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
$object | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
$object | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
$object | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
$object | Add-Member Noteproperty ProductKey -value $STR
Write-Output $object
}
catch
{
Write-Error $_
}
}
}