refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / wsl / install-wsl.ps1
1 #!/usr/bin/env pwsh
2
3 echo "Installing 1 of 5 Microsoft-Windows-Subsystem-Linux (for WSL 1 and WSL 2) ..."
4 & dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
5
6 echo ""
7 echo "Installing 2 of 5 VirtualMachinePlatform (for WSL 2 Hyper-V) ..."
8 & dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
9
10 Function Test-CommandExists
11 {
12     Param ($command)
13     $oldPreference = $ErrorActionPreference
14     $ErrorActionPreference = 'stop'
15     try {if(Get-Command $command){RETURN $true}}
16     Catch {RETURN $false}
17     Finally {$ErrorActionPreference=$oldPreference}
18 }
19
20 echo ""
21 IF(!(Test-CommandExists wsl))
22 {
23     echo "Skipping 3 of 5: Microsoft Linux Kernel requires WSL 1 to be installed first ..."
24 }
25 ELSE
26 {
27     echo "Installing 3 of 5 Microsoft Linux Kernel (wsl_update_x64.msi for WSL 2) ..."
28     IF (!(Test-Path -Path "$Env:TEMP\wsl_update_x64.msi")) {
29         & curl.exe -f -o "$Env:TEMP\wsl_update_x64.msi" "https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi"
30     }
31     IF (!(Test-Path -Path "C:\Temp\System32\lxss\tools\kernel")) {
32         # NOTE: This WILL NOT work with TARGETDIR=$Env:TEMP!!
33         echo "Start-Process msiexec -Wait -ArgumentList '/a ""$Env:TEMP\wsl_update_x64.msi"" /quiet /qn TARGETDIR=""C:\Temp""'"
34         powershell -Command "Start-Process msiexec -Wait -ArgumentList '/a ""$Env:TEMP\wsl_update_x64.msi"" /quiet /qn TARGETDIR=""C:\Temp""'"
35         echo "Unpacked to C:\Temp\System32\lxss\tools\kernel"
36     }
37     Copy-Item -Path "C:\Temp\System32\lxss" -Destination "C:\System32" -Recurse -Force
38     echo "Copied to C:\System32\lxss\tools\kernel ..."
39
40     echo "Start-Process msiexec -Wait -ArgumentList '/i','$Env:TEMP\wsl_update_x64.msi','/quiet','/qn'"
41     powershell -Command "Start-Process msiexec -Wait -ArgumentList '/i','$Env:TEMP\wsl_update_x64.msi','/quiet','/qn'"
42 }
43
44 Start-Sleep -s 3