echo "Installing 2 of 3 VirtualMachinePlatform ..."
& dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
+Function Test-CommandExists
+{
+ Param ($command)
+ $oldPreference = $ErrorActionPreference
+ $ErrorActionPreference = 'stop'
+ try {if(Get-Command $command){RETURN $true}}
+ Catch {Write-Host “$command does not exist”; RETURN $false}
+ Finally {$ErrorActionPreference=$oldPreference}
+}
+
echo ""
-echo "Installing 3 of 3 Microsoft Linux Kernel (wsl_update_x64.msi) ..."
-& curl.exe -f -o wsl_update_x64.msi "https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi"
-# TODO could we do /quiet /qn to get rid of the popup?
-powershell -Command "Start-Process msiexec -Wait -ArgumentList '/a wsl_update_x64.msi /quiet /qn TARGETDIR=""$env:TEMP""'"
-#& msiexec /a "wsl_update_x64.msi" /qb TARGETDIR="$env:TEMP"
-#Start-Sleep -s 10
-echo "Copied to $env:TEMP"
+IF(!(Test-CommandExists wsl))
+{
+ echo "Skipping 3 of 3: Microsoft Linux Kernel requires WSL 1 to be installed first ..."
+}
+ELSE
+{
+ echo "Installing 3 of 3 Microsoft Linux Kernel (wsl_update_x64.msi) ..."
+
+ IF (!(Test-Path -Path "$Env:TEMP\wsl_update_x64.msi")) {
+ & curl.exe -f -o "$Env:TEMP\wsl_update_x64.msi" "https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi"
+ }
+ IF (!(Test-Path -Path "C:\Temp\System32\lxss\tools\kernel")) {
+ # NOTE: This WILL NOT work with TARGETDIR=$Env:TEMP!!
+ echo "Start-Process msiexec -Wait -ArgumentList '/a ""$Env:TEMP\wsl_update_x64.msi"" /quiet /qn TARGETDIR=""C:\Temp""'"
+ powershell -Command "Start-Process msiexec -Wait -ArgumentList '/a ""$Env:TEMP\wsl_update_x64.msi"" /quiet /qn TARGETDIR=""C:\Temp""'"
+ echo "Unpacked to C:\Temp\System32\lxss\tools\kernel"
+ }
+ Copy-Item -Path "C:\Temp\System32\lxss" -Destination "C:\System32" -Recurse -Force
+ echo "Copied to C:\System32\lxss\tools\kernel ..."
-Copy-Item -Path "$env:TEMP\System32\lxss" -Destination "C:\System32" -Recurse
-echo "Installed C:\System32\lxss\tools\kernel ..."
+ echo "Start-Process msiexec -Wait -ArgumentList '/i','$Env:TEMP\wsl_update_x64.msi','/quiet','/qn'"
+ powershell -Command "Start-Process msiexec -Wait -ArgumentList '/i','$Env:TEMP\wsl_update_x64.msi','/quiet','/qn'"
+}
-Start-Sleep -s 2
+Start-Sleep -s 3