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