diff --git a/wsl/wsl_install.ps1 b/wsl/wsl_install.ps1 index da77d53c..a20f5e4e 100644 --- a/wsl/wsl_install.ps1 +++ b/wsl/wsl_install.ps1 @@ -1,3 +1,4 @@ +#Requires -RunAsAdministrator <# .SYNOPSIS Install and set up the specified WSL distro. @@ -64,20 +65,28 @@ begin { } process { + # *Check if WSL is updated + wsl.exe --update + + # *Check the current default version + $wslDefaultVersion = Get-ItemPropertyValue -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss' -Name 'DefaultVersion' -ErrorAction SilentlyContinue + if ($wslDefaultVersion -ne 2) { + Write-Warning 'You are currently using WSL version 1 as default.' + if ((Read-Host -Prompt 'Would you like to switch to WSL 2 (recommended)? [Y/n]') -ne 'n') { + Write-Host 'Setting the default version to WSL 2.' + wsl.exe --set-default-version 2 + } else { + Write-Host 'Keeping the default WSL 1 version.' + } + } + # *Install PowerShell try { Get-Command pwsh.exe -CommandType Application | Out-Null } catch { - $scriptPath = Resolve-Path wsl/pwsh_setup.ps1 - if (Test-IsAdmin) { - & $scriptPath - # update environment paths - Update-SessionEnvironmentPath - } else { - Start-Process powershell.exe "-NoProfile -File `"$scriptPath`"" -Verb RunAs - Write-Host "`nInstalling PowerShell Core. Complete the installation and run the script again!`n" -ForegroundColor Yellow - exit 0 - } + wsl/pwsh_setup.ps1 + # update environment paths + Update-SessionEnvironmentPath } # *Set up WSL @@ -91,10 +100,9 @@ process { $reposStr = $Repos | Join-Str -Separator ',' -SingleQuote $sb.Append(" -Repos @($reposStr)") | Out-Null } - if ($AddCertificate) { $sb.Append(" -AddCertificate") | Out-Null } + if ($AddCertificate) { $sb.Append(' -AddCertificate') | Out-Null } $sb.Append(" -OmpTheme 'base'") | Out-Null # run the wsl_setup script - Write-Host '*** WSL Setup ***' -ForegroundColor White pwsh.exe -NoProfile -Command $sb.ToString() } diff --git a/wsl/wsl_setup.ps1 b/wsl/wsl_setup.ps1 index 5e156844..c0454f99 100644 --- a/wsl/wsl_setup.ps1 +++ b/wsl/wsl_setup.ps1 @@ -134,7 +134,9 @@ begin { $lxss = Get-WslDistro | Where-Object Name -NotMatch '^docker-desktop' if ($PsCmdlet.ParameterSetName -ne 'Update') { if ($Distro -notin $lxss.Name) { - $onlineDistros = Get-WslDistro -Online + for ($i = 0; $i -lt 5; $i++) { + if ($onlineDistros = Get-WslDistro -Online) { break } + } # install online distro if ($Distro -in $onlineDistros.Name) { Write-Host "`nspecified distribution not found ($Distro), proceeding to install..." -ForegroundColor Cyan @@ -160,6 +162,19 @@ begin { } } # *perform initial distro setup + # disable appending Windows path inside distro to fix mounting issues + $lxss = Get-WslDistro -FromRegistry | Where-Object Name -EQ $Distro + # check if current version is V2 + if ($lxss.Version -eq 1) { + Write-Warning "The distribution `"$Distro`" is currently using version 1." + if ((Read-Host -Prompt 'Would you like to convert it to version 2 (recommended)? [Y/n]') -ne 'n') { + Write-Host 'Converting the distro to version 2.' + wsl --set-version $Distro 2 + $lxss.Version = 2 + } else { + Write-Host 'Keeping the version 1 for the distribution.' + } + } # enable automount in wsl.conf $param = @{ Distro = $Distro @@ -171,10 +186,9 @@ begin { } } Set-WslConf @param - # disable appending Windows path inside distro to fix mounting issues - $lxss = Get-WslDistro -FromRegistry | Where-Object Name -EQ $Distro - if ($lxss -and $lxss.Flags -ne 13) { - Set-ItemProperty -Path $lxss.PSPath -Name 'Flags' -Value 13 + if ($lxss -and $lxss.Flags -notin @(5, 13)) { + $flag = $lxss.Version -eq 1 ? 5 : 13 + Set-ItemProperty -Path $lxss.PSPath -Name 'Flags' -Value $flag Write-Host "`nrestarting WSL to apply changes..." -ForegroundColor Cyan wsl.exe --shutdown $Distro }