-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewDevelopmentWSL.ps1
207 lines (159 loc) · 6.11 KB
/
NewDevelopmentWSL.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
Function Get-YesNoAnswer($question)
{
$msg = $question + " [y/n]"
do {
$response = Read-Host -Prompt $msg
if ($response -eq 'y') {
return $true
}
} until ($response -eq 'n')
return $false
}
Function Get-DistroImage($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "tar files | *.tar"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
Function Get-ValidDistroImageVisual()
{
Do {
$error_encountered = $false
$wsl_image_path = Get-DistroImage -initialDirectory $PWD
if($wsl_image_path -eq ""){
Write-Host "Selection aborted. Exiting script"
exit
}
} While ($error_encountered)
return $wsl_image_path
}
Function Get-ValidDistroImageCMD()
{
Do {
$error_encountered = $false
$wsl_image_path = Read-Host "Enter the path to the .tar with the WSL image you want to install"
$image_path_existst = Test-Path -Path $wsl_image_path -PathType Leaf
if(!($image_path_existst)){
$error_encountered = $true
Write-Host "ERROR: image file not found"
}
if(!([IO.Path]::GetExtension($wsl_image_path) -eq '.tar')){
$error_encountered = $true
Write-Host "ERROR: file not a .tar file"
}
} While ($error_encountered)
return $wsl_image_path
}
Function Get-InstallPath()
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.SelectedPath
}
Function Get-ValidInstallPathVisual()
{
Do {
$error_encountered = $false
$wsl_install_path = Get-InstallPath
if($wsl_install_path -eq $null){
Write-Host "Selection aborted. Exiting script"
exit
}
} While ($error_encountered)
return $wsl_install_path
}
Function Get-ValidInstallPathCMD()
{
Do {
$error_encountered = $false
$wsl_install_path = Read-Host "Enter the path where you want to install the WSL instance"
$install_path_existst = Test-Path -Path $wsl_install_path -PathType Container
if(!($install_path_existst)){
$error_encountered = $true
Write-Host "ERROR: folder not found"
}
} While ($error_encountered)
return $wsl_install_path
}
#---------------------------------
Write-Host "Development WSL manager (2021-07-12)"
Write-Host "------------------------------------"
Write-Host "DISCLAIMER: This script is not affiliated with Microsoft.`nI just wrote it to make creating and deleting (new) WSL instance easier.`n"
Do {
$script_mode= Read-Host "Delete existing or create new instance? [del/new]"
} While ($script_mode -notmatch "del|new")
if($script_mode -eq "new"){
Do {
$script_interaction_mode= Read-Host "Use graphical windows or only command line? [cmd/ui]"
} While ($script_interaction_mode -notmatch "cmd|ui")
# WSL Name
$wsl_name = Read-Host "Please enter the desired name for the new instance"
# WSL Version
# Only allow Version 1 or 2 as input
Do {
$wsl_version = Read-Host "Select WSL Version [1/2]"
} While ($wsl_version -notmatch "1|2")
# WSL .tar image
if($script_interaction_mode -eq "cmd"){
$wsl_image_path = Get-ValidDistroImageCMD
} else{
Write-Host "Select the .tar with the WSL image you want to install"
$wsl_image_path = Get-ValidDistroImageVisual
}
# WSL install location
if($script_interaction_mode -eq "cmd"){
$wsl_install_path = Get-ValidInstallPathCMD
} else{
Write-Host "Select the path where you want to install the WSL instance"
$wsl_install_path = Get-ValidInstallPathVisual
}
Write-Host "`n$("Creating WSL instance *")$wsl_name$("*")"
$wsl_create_return = wsl --import $wsl_name $wsl_install_path $wsl_image_path
if($wsl_create_return -eq $null){
Write-Host "`n$("Created WSL instance *")$wsl_name$("*")"
Write-Host "`nBeginning post-install setup`n"
Do {
$dist_type= Read-Host "Please select the type of the distribution [debian-ubuntu/other]"
} While ($dist_type -notmatch "debian-ubuntu|other")
if($dist_type -eq "debian-ubuntu"){
$deb_username = Read-Host "Please enter the desired UNIX username"
Write-Host "`nCreating user"
# Add user
wsl -d $wsl_name adduser $deb_username
# Add sudo privileges
Write-Host "`nAdding user to the sudo group"
wsl -d $wsl_name sudo usermod -a -G sudo $deb_username
# Set standard user to the new user
Write-Host "`nCreating wsl.conf with new user set as standard user"
$wsl_conf = "$("printf '[user]\ndefault=")$($deb_username)$("\n' > /etc/wsl.conf")"
wsl -d $wsl_name /bin/bash -c "`"$wsl_conf`""
# "Restart" WSL instance
Write-Host "`nRestarting WSL instance"
wsl --terminate $wsl_name
# Install updates
if(Get-YesNoAnswer("Do you want to search for updates and install them?")){
wsl -d $wsl_name /bin/bash -c 'sudo apt-get update && sudo apt-get dist-upgrade -y'
}
}else{
Write-Host "Distribution isn't supported by this script. Please perform the post-install setup manually."
}
}else{
Write-Host $wsl_create_return
Write-Error "unknown error"
}
}else{
# WSL Name
$wsl_name = Read-Host "Please enter the name for the instance you want to delete"
$delete_confirmation = Get-YesNoAnswer("Do you really want to delete " + $wsl_name + "?")
if($delete_confirmation){
Write-Host "$("Deleting ")$($wsl_name)"
wsl --unregister $wsl_name
}
}
Read-Host -Prompt "`nPress any key to continue..."