-
Notifications
You must be signed in to change notification settings - Fork 23
/
Install_WSL_EasyGitian.ps1
122 lines (110 loc) · 4.9 KB
/
Install_WSL_EasyGitian.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
<#
.SYNOPSIS
Installs Windows subsystem for linux (WSL) option if not already configured. Also downloads and starts the distro setup.
.DESCRIPTION
Installs Windows subsystem for linux (WSL) option if not already configured. Also downloads and starts the distro setup. Supports ubuntu, sles, and opensuse.
.PARAMETER InstallPath
Path to install chosen WSL distribution
.PARAMETER Distro
Distro to attempt to download and install
.EXAMPLE
.\Install-WSL.ps1
Configures the WSL feature if required then attempts to install the ubuntu wsl distribution to C:\WSLDistros\Ubuntu
.NOTES
Author: Zachary Loeber
- The downloads are skipped if already found in the $env:temp directory.
- The installer process may fail without a reboot between the feature install and the distro installer running.
- Unregister or manage the default distro install via wslconfig.exe
.LINK
https://docs.microsoft.com/en-us/windows/wsl/install-on-server
#>
[CmdletBinding()]
param(
[Parameter(HelpMessage = 'Path to save and install WSL distro to.')]
[string]$InstallPath = 'C:\WSLDistros\Ubuntu',
[Parameter(HelpMessage = 'Distro to attempt to download and install')]
[ValidateSet('ubuntu', 'opensuse', 'sles')]
[string]$Distro = 'ubuntu'
)
Begin {
$WSLDownloadPath = Join-Path $ENV:TEMP "$Distro.zip"
$DistroURI = @{
'ubuntu' = 'https://aka.ms/wsl-ubuntu-1604'
'sles' = 'https://aka.ms/wsl-sles-12'
'opensuse' = 'https://aka.ms/wsl-opensuse-42'
}
$DistroEXE = @{
'ubuntu' = 'ubuntu.exe'
'sles' = 'SLES-12.exe'
'opensuse' = 'openSUSE-42.exe'
}
function Start-Proc {
param([string]$Exe = $(Throw "An executable must be specified"),
[string]$Arguments,
[switch]$Hidden,
[switch]$waitforexit)
$startinfo = New-Object System.Diagnostics.ProcessStartInfo
$startinfo.FileName = $Exe
$startinfo.Arguments = $Arguments
if ($Hidden) {
$startinfo.WindowStyle = 'Hidden'
$startinfo.CreateNoWindow = $True
}
$process = [System.Diagnostics.Process]::Start($startinfo)
if ($waitforexit) { $process.WaitForExit() }
}
Function ReRunScriptElevated {
if ( -not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator') ) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" $PSCommandArgs" -WorkingDirectory $pwd -Verb RunAs
Exit
}
}
$MyExe = Join-Path $InstallPath $DistroEXE[$Distro]
Write-Host "Checking for EXE $MyExe - Distro $DistroEXE[$Distro] in Path $InstallPath"
if (Test-Path $MyExe) {
Start-Process $MyExe -ArgumentList 'run wget -N -O $HOME/Install_EasyGitian.sh https://raw.githubusercontent.com/mazaclub/easygitianbuilder/v0.1.0/Install_EasyGitian.sh'
Start-Process $MyExe
#Exit
}
else {
ReRunScriptElevated
}
}
end {
if ((Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux).State -ne 'Enabled') {
try {
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
#Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart
}
catch {
Write-Warning 'Unable to install the WSL feature!'
}
}
else {
Write-Output 'Windows subsystem for Linux optional feature already installed!'
}
$InstalledWSLDistros = @((Get-ChildItem 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss' -ErrorAction:SilentlyContinue | ForEach-Object { Get-ItemProperty $_.pspath }).DistributionName)
$WSLExe = Join-Path $InstallPath $DistroEXE[$Distro]
if ($InstalledWSLDistros -notcontains $Distro) {
Write-Output "WSL distro $Distro is not found to be installed on this system, attempting to download and install it now..."
if (-not (Test-Path $WSLDownloadPath)) {
Invoke-WebRequest -Uri $DistroURI[$Distro] -OutFile $WSLDownloadPath -UseBasicParsing
}
else {
Write-Warning "The $Distro zip file appears to already be downloaded."
}
Expand-Archive $WSLDownloadPath $InstallPath -Force
if (Test-Path $WSLExe) {
Write-Output "Starting $WSLExe"
Start-Proc -Exe $WSLExe -waitforexit
}
else {
Write-Warning " $WSLExe was not found for whatever reason"
}
}
else {
Write-Warning "Found $Distro is already installed on this system. Enter it simply by typing bash.exe"
#Start-Process powershell.exe -Credential MSEDGEWIN10\IEUser -ArgumentList '-noprofile -command & {Start-Process C:\WSLDistros\Ubuntu\ubuntu.exe -verb runAs}'
#Start-Process runas.exe -ArgumentList '/trustlevel:0x20000 C:\WSLDistros\Ubuntu\ubuntu.exe'
}
}