-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreeActivate.psm1
128 lines (107 loc) · 4.15 KB
/
FreeActivate.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
# FreeActivate.psm1
$ipRegex = '^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$'
$fqdnRegex = '^(?=.{1,255}$)([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,}$'
$windowsKeyRegex = '^[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}-[A-Za-z0-9]{5}$'
$slmgrPath = Join-Path -Path $env:SystemRoot -ChildPath "System32\slmgr.vbs"
function Get-Activation() {
$licenseInfo = (cscript.exe /NoLogo $script:slmgrPath /dlv) -Split "`n"
$licenseStatus = $licenseInfo | ForEach-Object {
if ($_ -like "*License Status*") {
$_ -replace ".*License Status: ", ""
}
}
$licenseObject = [pscustomobject]@{
LicenseStatus = $licenseStatus
}
return $licenseObject
}
function Set-KmsActivation() {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
param(
[Parameter(Mandatory=$true)]
[string]$Server,
[Parameter(Mandatory=$true)]
[string]$Key
)
$Server.Replace(' ', '')
$Key.Replace(' ', '')
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentPrincipal = new-object Security.Principal.WindowsPrincipal($currentUser)
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
throw "This command requires administrative privileges"
}
if ([string]::IsNullOrEmpty($Server) -or [string]::IsNullOrEmpty($Key)) {
throw "Key and Server parameters cannot be empty or have a null value."
}
if ($Server -notmatch $script:ipRegex -and $Server -notmatch $script:fqdnRegex) {
throw "Server parameter does not match IP or FQDN format"
}
if ($Key -notmatch $script:windowsKeyRegex) {
throw "Incorrect Windows Key Format"
}
if (-not (Test-Connection -ComputerName $Server -Count 2 -Quiet)) {
throw "No route to KMS Server"
}
if (-not (Test-NetConnection -ComputerName $Server -Port 1688)) {
throw "Port 1688 not open on KMS Server"
}
$activationKey = $Key.ToUpper()
if ($PSCmdlet.ShouldProcess("KMS Server Configuration", "Set KMS Server to $Server and install activation key $Key")) {
try {
cscript.exe /NoLogo $script:slmgrPath /ipk $activationKey
if ($LASTEXITCODE -ne 0) {
throw "Error installing product key. Exit Code: $LASTEXITCODE"
}
cscript.exe /NoLogo $script:slmgrPath /skms $Server
if ($LASTEXITCODE -ne 0) {
throw "Error setting KMS Server. Exit Code: $LASTEXITCODE"
}
cscript.exe /NoLogo $script:slmgrPath /ato
if ($LASTEXITCODE -ne 0) {
throw "Error activating Windows. Exit Code: $LASTEXITCODE"
}
$activationStatus = Get-Activation
return $activationStatus
} catch {
throw "Error during licensing operation: $_"
}
}
}
function Set-MakActivation() {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High')]
param(
[Parameter(Mandatory=$true)]
[string]$Key
)
$Key.Replace(' ', '')
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentPrincipal = new-object Security.Principal.WindowsPrincipal($currentUser)
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
throw "This command requires administrative privileges"
}
if ([string]::IsNullOrEmpty($Key)) {
throw "Key parameter cannot be empty or have a null value."
}
if ($Key -notmatch $script:windowsKeyRegex) {
throw "Incorrect Windows Key Format"
}
$activationKey = $Key.ToUpper()
if ($PSCmdlet.ShouldProcess("Install Multiple Activation Key", "Set MAK Key and install activation key $Key")) {
try {
cscript.exe /NoLogo $script:slmgrPath /ipk $activationKey
if ($LASTEXITCODE -ne 0) {
throw "Error installing product key. Exit Code: $LASTEXITCODE"
}
cscript.exe /NoLogo $script:slmgrPath /ato
if ($LASTEXITCODE -ne 0) {
throw "Error activating Windows. Exit Code: $LASTEXITCODE"
}
$activationStatus = Get-Activation
return $activationStatus
} catch {
throw "Error during licensing operation: $_"
}
}
}