-
Notifications
You must be signed in to change notification settings - Fork 0
/
00_WSL2_prerequisite.ps1
138 lines (111 loc) · 4.72 KB
/
00_WSL2_prerequisite.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
# Copyright © 2024 @adithyasunil04
#Print ASCII
clear
Write-Host " &BGBB#& #B#&
&#GPGGB# BGG#
#GGGGB#BGGG#
#GGGGGGGGGG&
&BGGGGGGGGGGBBB###BB&
BGPPGG5J7!!7Y5PPPY7YB&
#G?~557^:....:^^~?PP5GG&
&B5!:^!^::......~?JYBBBGGG#
#G5~:::::......:5&@@@@@@@&##&
BGP!:::::......!B
#GG?:::::.....:J&
&GGP~::::.....!G
BGG5^::::...~5
#GGG5^:::...!#
#GGGGP~:::...P
&&#BGGGGGG7:::..^#
&&#BGY?!~~!7J5J:::..~&
&######BGPPP555PPJ^::.!&
#5!::~JG&
&GJ7JY55#
&&"
Write-Host "
_/ _/ _/ _/_/_/ _/ _/ _/_/_/ _/_/_/_/_/ _/_/_/
_/ _/ _/ _/ _/ _/ _/ _/ _/
_/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/ _/ _/ _/ _/
_/_/_/_/ _/_/ _/_/_/ _/ _/_/_/ _/ _/_/_/
"
Write-Host
Write-Host
function Centered-Text {
param (
[string]$Text, # Text to be centered
[string]$Padding = "=" # Padding character, default is "="
)
# Get the width of the terminal window
$windowWidth = [console]::WindowWidth
# Calculate the total length of the output line
$textLength = $Text.Length
$paddingLength = $Padding.Length
# Calculate the number of padding blocks needed on each side
$availableSpace = $windowWidth - $textLength
$padCount = [math]::Floor($availableSpace / (2 * $paddingLength))
# Build the line with padding and centered text
$line = ($Padding * $padCount) + " " + $Text + ($Padding * $padCount)
# Print the result
Write-Output $line
Write-Host
Write-Host
Write-Host
}
Start-Sleep -Seconds 1
# Call the function with user input
Centered-Text -Text "Kali Linux WSL2 installer: Script 00"
# Check if running with admin privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Centered-Text -Text "Please run this script as an administrator." -Padding " "
Write-Host
Start-Sleep -Seconds 1
exit
}
# Check if WSL is already enabled
$wslFeature = Get-WindowsOptionalFeature -Online -FeatureName 'Microsoft-Windows-Subsystem-Linux'
$vmFeature = Get-WindowsOptionalFeature -Online -FeatureName 'VirtualMachinePlatform'
$restartNeeded = $false
# If both WSL2 and VirtualMachinePlatform are already enabled
if ($wslFeature.State -eq 'Enabled' -and $vmFeature.State -eq 'Enabled') {
# Check if either of them requires a restart to be fully functional
if ($wslFeature.RestartRequired -eq 'Possible' -or $vmFeature.RestartRequired -eq 'Possible') {
Write-Host "-[*] WSL2 and VirtualMachinePlatform already enabled. But restart is perhaps required to start using it."
$restartNeeded = $true
} else {
Write-Host "-[*] WSL2 and VirtualMachinePlatform already enabled."
exit
}
}
# Enable WSL if not already enabled
if ($wslFeature.State -ne 'Enabled') {
Enable-WindowsOptionalFeature -Online -FeatureName 'Microsoft-Windows-Subsystem-Linux' -All -NoRestart
Write-Host "-[*] WSL2 Feature Installation has NOW been Enabled"
$restartNeeded = $true
} else {
Write-Host "-[*] WSL2 is already enabled."
}
# Enable VirtualMachinePlatform if not already enabled
if ($vmFeature.State -ne 'Enabled') {
Enable-WindowsOptionalFeature -Online -FeatureName 'VirtualMachinePlatform' -All -NoRestart
Write-Host "-[*] VirtualMachinePlatform Feature Installation Enabled"
$restartNeeded = $true
} else {
Write-Host "-[*] VirtualMachinePlatform is already enabled."
}
# If either WSL or VirtualMachinePlatform was installed or restart is needed
if ($restartNeeded) {
Write-Host "ALERT: Restart perhaps required to apply changes. WSL2 and VirtualMachinePlatform Program Files will be enabled with Windows-Update when you restart system."
$selection = Read-Host "RESTART your PC before executing the next install script. Restart? [(Y)es/(N)o def=N]"
# Check if the input is 'y' or 'Y'
if ($selection -eq 'y' -or $selection -eq 'Y') {
Write-Output "Restarting system..."
Start-Sleep -Seconds 1
Restart-Computer
} else {
Centered-Text -Text "ALERT: Restart is perhaps required to complete the setup. Please RESTART your PC before executing the next install script."
exit
}
} else {
Centered-Text -Text "No restart required, both features are already enabled and running."
}