-
Notifications
You must be signed in to change notification settings - Fork 30
/
WSL2_Install.ps1
381 lines (370 loc) · 15.2 KB
/
WSL2_Install.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# Install WSL
# This script needs to be run as a priviledged user
Write-Host("Checking for Windows Subsystem for Linux...")
$rebootRequired = $false
if ((Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux).State -ne 'Enabled'){
Write-Host(" ...Installing Windows Subsystem for Linux.")
$wslinst = Enable-WindowsOptionalFeature -Online -NoRestart -FeatureName Microsoft-Windows-Subsystem-Linux
if ($wslinst.Restartneeded -eq $true){
$rebootRequired = $true
}
} else {
Write-Host(" ...Windows Subsystem for Linux already installed.")
}
Write-Host("Checking for Virtual Machine Platform...")
if ((Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform).State -ne 'Enabled'){
Write-Host(" ...Installing Virtual Machine Platform.")
$vmpinst = Enable-WindowsOptionalFeature -Online -NoRestart -FeatureName VirtualMachinePlatform
if ($vmpinst.RestartNeeded -eq $true){
$rebootRequired = $true
}
} else {
Write-Host(" ...Virtual Machine Platform already installed.")
}
function Update-Kernel () {
Write-Host(" ...Downloading WSL2 Kernel Update.")
$kernelURI = 'https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi'
$kernelUpdate = ((Get-Location).Path) + '\wsl_update_x64.msi'
(New-Object System.Net.WebClient).DownloadFile($kernelURI, $kernelUpdate)
Write-Host(" ...Installing WSL2 Kernel Update.")
msiexec /i $kernelUpdate /qn
Start-Sleep -Seconds 5
Write-Host(" ...Cleaning up Kernel Update installer.")
Remove-Item -Path $kernelUpdate
}
function Get-Kernel-Updated () {
# Check for Kernel Update Package
Write-Host("Checking for Windows Subsystem for Linux Update...")
$uninstall64 = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | ForEach-Object { Get-ItemProperty $_.PSPath } | Select-Object DisplayName, Publisher, DisplayVersion, InstallDate
if ($uninstall64.DisplayName -contains 'Windows Subsystem for Linux Update') {
return $true
} else {
return $false
}
}
$pkgs = (Get-AppxPackage).Name
function Get-WSLlist {
$wslinstalls = New-Object Collections.Generic.List[String]
$(wsl -l) | ForEach-Object { if ($_.Length -gt 1){ $wslinstalls.Add($_) } }
$wslinstalls = $wslinstalls | Where-Object { $_ -ne 'Windows Subsystem for Linux Distributions:' }
return $wslinstalls
}
function Get-WSLExistance ($distro) {
# Check for the existence of a distro
# return Installed as Bool
$wslImport = $false
if (($distro.AppxName).Length -eq 0){ $wslImport = $true }
$installed = $false
if ( $wslImport -eq $false ){
if ($pkgs -match $distro.AppxName) {
$installed = $true
}
} else {
if (Get-WSLlist -contains ($distro.Name).Replace("-", " ")){
$installed = $true
}
}
return $installed
}
function Get-StoreDownloadLink ($distro) {
# Uses $distro.StoreLink to get $distro.URI
# Required when URI is not hard-coded
#### Thanks to MattiasC85 for this excelent method of getting Microsoft Store download URIs ####
# Source: https://github.com/MattiasC85/Scripts/blob/a1163b97875ed075927438505808622614a9961f/OSD/Download-AppxFromStore.ps1
$wchttp=[System.Net.WebClient]::new()
$URI = "https://store.rg-adguard.net/api/GetFiles"
$myParameters = "type=url&url=$($distro.StoreLink)"
$wchttp.Headers[[System.Net.HttpRequestHeader]::ContentType]="application/x-www-form-urlencoded"
$HtmlResult = $wchttp.UploadString($URI, $myParameters)
$Start=$HtmlResult.IndexOf("<p>The links were successfully received from the Microsoft Store server.</p>")
if ($Start -eq -1) {
write-host "Could not get Microsoft Store download URI, please check the StoreURL."
exit
}
$TableEnd=($HtmlResult.LastIndexOf("</table>")+8)
$SemiCleaned=$HtmlResult.Substring($start,$TableEnd-$start)
$newHtml=New-Object -ComObject "HTMLFile"
$src = [System.Text.Encoding]::Unicode.GetBytes($SemiCleaned)
$newHtml.write($src)
$ToDownload=$newHtml.getElementsByTagName("a") | Select-Object textContent, href
$apxLinks = @()
$ToDownload | Foreach-Object {
if ($_.textContent -match '.appxbundle') {
$apxLinks = $_
}
}
$distro.URI = $apxLinks.href
return $distro
}
function Check-Sideload (){
# Return $true if sideloading is enabled
$keyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
$Key = Get-Item -LiteralPath $keyPath
$sideloadKeys = @("AllowAllTrustedApps", "AllowDevelopmentWithoutDevLicense")
$return = $true
function Test-RegProperty ($propertyname){
if (($Key.GetValue($propertyname, $null)) -ne $null){
return $true
} else {
return $false
}
}
$sideloadKeys | ForEach-Object {
if (!(Test-RegProperty ($_))){
$return = $false
} else {
if (( (Get-ItemProperty -Path $keyPath -Name $_).$_ ) -ne 1 ){
$return = $false
}
}
}
return $return
}
function Enable-Sideload () {
# Allow sideloading of unsigned appx packages
$keyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock"
if (!(Test-Path -Path $keyPath)){
New-Item -Path $keyPath # In case the entire registry key was accidentally deleted
}
$Key = Get-Item -LiteralPath $keyPath
$sideloadKeys = @("AllowAllTrustedApps", "AllowDevelopmentWithoutDevLicense")
function Test-RegProperty ($propertyname){
if (($Key.GetValue($propertyname, $null)) -ne $null){
return $true
} else {
return $false
}
}
$sideloadKeys | ForEach-Object {
if (!(Test-RegProperty $_)){
New-ItemProperty -Path $keyPath -Name $_ -Value "1" -PropertyType DWORD -Force | Out-Null
} else {
Set-ItemProperty -Path $keyPath -Name $_ -Value "1" -PropertyType DWORD -Force | Out-Null
}
}
}
function Select-Distro () {
# See: https://docs.microsoft.com/en-us/windows/wsl/install-manual
# You can also use https://store.rg-adguard.net to get Appx links from Windows Store links
$distrolist = (
[PSCustomObject]@{
'Name' = 'Ubuntu 24.04'
'URI' = 'http://tlu.dl.delivery.mp.microsoft.com/filestreamingservice/files/c63f9075-a155-4349-8fe2-553204ea0907?P1=1715652771&P2=404&P3=2&P4=MH5ZLlMqG0KhjPQRJwGkoVU4bIB3y3uIYHE%2fpTePAcdXT9a5wEesuJiW4zWazIhYcx41id4VFHjGsra7FxQBTA%3d%3d'
'AppxName' = 'CanonicalGroupLimited.Ubuntu24.04onWindows'
'winpe' = 'ubuntu2404.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Ubuntu 22.04'
'URI' = 'https://aka.ms/wslubuntu2204'
'AppxName' = 'CanonicalGroupLimited.Ubuntu22.04onWindows'
'winpe' = 'ubuntu2204.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Ubuntu 20.04'
'URI' = 'https://aka.ms/wslubuntu2004'
'AppxName' = 'CanonicalGroupLimited.Ubuntu20.04onWindows'
'winpe' = 'ubuntu2004.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Ubuntu 18.04'
'URI' = 'https://aka.ms/wsl-ubuntu-1804'
'AppxName' = 'CanonicalGroupLimited.Ubuntu18.04onWindows'
'winpe' = 'ubuntu1804.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Ubuntu 16.04'
'URI' = 'https://aka.ms/wsl-ubuntu-1604'
'AppxName' = 'CanonicalGroupLimited.Ubuntu16.04onWindows'
'winpe' = 'ubuntu1604.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Debian'
'URI' = 'https://aka.ms/wsl-debian-gnulinux'
'AppxName' = 'TheDebianProject.DebianGNULinux'
'winpe' = 'debian.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Kali'
'URI' = 'https://aka.ms/wsl-kali-linux-new'
'AppxName' = 'KaliLinux'
'winpe' = 'kali.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'openSUSE Leap 42'
'URI' = 'https://aka.ms/wsl-opensuse-42'
'AppxName' = 'openSUSELeap42'
'winpe' = 'openSUSE-42.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'openSUSE Leap 15.2'
'URI' = 'https://aka.ms/wsl-opensuseleap15-2'
'AppxName' = 'openSUSELeap15'
'winpe' = 'openSUSE-Leap-15.2.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'SUSE Linux Enterprise Server 12'
'URI' = 'https://aka.ms/wsl-sles-12'
'AppxName' = 'SUSELinuxEnterpriseServer12'
'winpe' = 'SLES-12.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'SUSE Linux Enterprise Server 15'
'URI' = 'https://aka.ms/wsl-SUSELinuxEnterpriseServer15SP2'
'AppxName' = 'SUSELinuxEnterpriseServer15SP2'
'winpe' = 'SUSE-Linux-Enterprise-Server-15-SP2.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Alpine'
'StoreLink' = 'https://www.microsoft.com/en-us/p/alpine-wsl/9p804crf0395'
'URI' = ''
'AppxName' = 'AlpineWSL'
'winpe' = 'Alpine.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Fedora Remix for WSL'
'URI' = 'https://github.com/WhitewaterFoundry/Fedora-Remix-for-WSL/releases/download/31.5.0/Fedora-Remix-for-WSL_31.5.0.0_x64_arm64.appxbundle'
'AppxName' = 'FedoraRemix'
'winpe' = 'fedoraremix.exe'
'installed' = $false
'sideloadreqd' = $true
}
# [PSCustomObject]@{
# 'Name' = 'Ubuntu 20.04'
# 'URI' = 'https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-wsl.rootfs.tar.gz'
# 'AppxName' = '' # Leave blank for wsl --import install
# 'winpe' = ''
# 'installed' = $false
# }
)
$distrolist | ForEach-Object { $_.installed = Get-WSLExistance($_) }
Write-Host("+------------------------------------------------+")
Write-Host("| Choose your Distro |")
Write-Host("+------------------------------------------------+")
For ($i = 0; $i -le ($distrolist.Length - 1); $i++) {
$installedTxt = ""
if (($distrolist.installed)[$i]) {
$installedTxt = "(Installed)"
}
Write-Host(($i + 1).ToString() + " " + ($distrolist.Name)[$i] + " " + $installedTxt)
}
[Int]$distroChoice = Read-Host '>'
$choiceNum = 0
if (($distroChoice.Length -ne 0) -and ($distroChoice -match '^\d+$')) {
if (($distroChoice -gt 0) -and ($distroChoice -le $distrolist.Length)) {
$choiceNum = ($distroChoice - 1)
}
}
$choice = $distrolist[$choiceNum]
return $choice
}
function Install-Distro ($distro) {
function Import-WSL ($distro) {
$distroinstall = "$env:LOCALAPPDATA\lxss"
$wslname = $($distro.Name).Replace(" ", "-")
$Filename = "$env:temp\" + $wslname + ".rootfs.tar.gz"
Write-Host(" ...Downloading " + $distro.Name + ".")
$client = New-Object net.WebClient
$client.DownloadFile($distro.URI, $Filename)
if (Test-Path $Filename){
Write-Host(" ...Importing " + $distro.Name + ".")
wsl.exe --import $wslname $distroinstall $Filename
} else {
Write-Host("ERROR: Cannot install $($distro.Name) missing rootfs.tar.gz package. (Download failed)") -ForegroundColor Red
}
}
function Add-WSLAppx ($distro) {
$Filename = "$env:temp\" + "$($distro.AppxName).appx"
$abortInstall = $false
if (($distro.sideloadreqd -eq $true) -and (!(Check-Sideload))){
Write-Host ("Sideloading must be turned on in order to install $($distro.Name)")
$allowEnable = (Read-Host ("Really enable sideloading? [Y/n]")).ToLower()
if ($allowEnable.Length -gt 1){ $allowEnable = $allowEnable.Substring(0,1) }
if (!($allowEnable -eq 'n')){
Write-Host ("Enabling sideloading...")
Enable-Sideload | Out-Null
} else {
$abortInstall = $true
}
}
if ($abortInstall -eq $false) {
Write-Host(" ...Downloading " + $distro.Name + ".")
if ($distro.URI.Length -lt 2) {
$distro = Get-StoreDownloadLink($distro) # Handle dynamic URIs
}
$client = New-Object net.WebClient
$client.DownloadFile($distro.URI, $Filename)
if (Test-Path $Filename) {
Write-Host(" ...Beginning " + $distro.Name + " install.")
Add-AppxPackage -Path $Filename
} else {
Write-Host("ERROR: Cannot install $($distro.Name) missing Appx package. (Download failed)") -ForegroundColor Red
}
Start-Sleep -Seconds 5
} else {
Write-Host("WARNING: Unable to install. Sideloading required, but not enabled.") -ForegroundColor Yellow
}
}
if (Get-WSLExistance($distro)) {
Write-Host(" ...Found an existing " + $distro.Name + " install")
} else {
if ($($distro.AppxName).Length -gt 1){
Add-WSLAppx($distro)
} else {
Import-WSL($distro)
}
}
}
if ($rebootRequired) {
shutdown /t 120 /r /c "Reboot required to finish installing WSL2"
$cancelReboot = Read-Host 'Cancel reboot for now (you still need to reboot and rerun to finish installing WSL2) [y/N]'
if ($cancelReboot.Length -ne 0){
if ($cancelReboot.Substring(0,1).ToLower() -eq 'y'){
shutdown /a
}
}
} else {
if (!(Get-Kernel-Updated)) {
Write-Host(" ...WSL kernel update not installed.")
Update-Kernel
} else {
Write-Host(" ...WSL update already installed.")
}
Write-Host("Setting WSL2 as the default...")
wsl --set-default-version 2
$distro = Select-Distro
Install-Distro($distro)
if ($distro.AppxName.Length -gt 1) {
if ($distro.sideloadreqd){
if (Check-Sideload){
Start-Process $distro.winpe
}
} else {
Start-Process $distro.winpe
}
} else {
$wslselect = ""
Get-WSLlist | ForEach-Object {
if ($_ -match $distro.Name){
$wslselect = $_
}
}
if ($wslselect -ne "") {
wsl -d $wslselect
} else {
Write-Host("Run 'wsl -l' to list WSL Distributions")
Write-Host("Run 'wsl -d <distroname>' to start WSL Distro")
}
}
}