Skip to content

Commit

Permalink
wsl-reset and sysdistrowt
Browse files Browse the repository at this point in the history
  • Loading branch information
sirredbeard committed Jun 3, 2023
1 parent 786c858 commit 211cf75
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@ wslctl disable pengwin
wslctl restart pengwin
```

/prompts contains example AI prompts used to generate the first draft of these scripts, the final scripts got hand polished, but they are there to see
## wsl-reset

A troubleshooting utility that resets WSL to various degrees.

`wsl-reset --reset` - shuts down WSL, resets the WSL service, and installs any WSL updates, if available
`wsl-reset --hard-reset` - shuts down WSL, stops the WSL service, uninstalls WSL, and re-installs WSL
`wsl-reset --destrutive-reset` - shuts down WSL, restarts the WSL service, **unregisters all WSL distros**, stops the WSL service, uninstalls WSL, and re-installs WSL

## sysdistrowt

Adds the WSL System Distro (CBL-Mariner) to the Windows Terminal and/or Windows Terminal Preview profiles, for easier debugging.

### /prompts folder

Contains example AI prompts used to generate the first draft of these scripts, the final scripts got hand polished, but they are there to see.
23 changes: 23 additions & 0 deletions prompts/sysdistrowt-prompt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Write a PowerShell 7 Core script that:

Checks for Windows Terminal in $env:LOCALAPPDATA "Packages"

Checks for a settings.json for Windows Terminal in $env:LOCALAPPDATA "Packages"

Checks for Windows Terminal Preview in $env:LOCALAPPDATA "Packages"

Checks for a settings.json for Windows Terminal Preview in $env:LOCALAPPDATA "Packages"

If neither Windows Terminal or Windows Terminal Preview are found, exit with a message that Windows Terminal is not installed.

If Windows Terminal is detected, but no settings.json for Windows Terminal is found, then run and close wt.exe from $env:ProgramFiles Microsoft.WindowsTerminal

If Windows Terminal Preview is detected, but no settings.json for Windows Terminal Preview is found, then run and close wt.exe from $env:ProgramFiles Microsoft.WindowsTerminal

Do not use wildcards to detect files or run wt.exe, parse the directory and use the exact folder name

Add an additional profile called "WSL System Distro" to Windows Terminal if it exists, that launches the WSL system distro with wsl.exe -u root --system

Add an additional profile called "WSL System Distro" to Windows Terminal Preview if it exists, that launches the WSL system distro with wsl.exe -u root --system

Checks if a profile created by the script already exists, and if so, removes the profile
11 changes: 11 additions & 0 deletions prompts/wsl-reset-prompt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Create a PowerShell 7 Core script that:

Checks if it is being run as administrator, and, if not, exists with a message that the script must be run as administrator.

If no argument is specified, the script should display a message that the script must be run with either --reset, --hard-reset, --destructive-reset.

If the --reset argument is specified, then run wsl.exe --shutdown, wsl.exe --update, and force restart the Windows Subsystem for Linux service in Windows.

If the --hard-reset argument is specified, then run wsl.exe --shutdown, uninstall Windows Subsystem for Linux, and then run wsl.exe --install.

If the --descrutive-reset argument is specified, then run wsl.exe --shutdown, run wsl.exe --unregister-all, uninstall Windows Subsystem for Linux, and then run and then run wsl.exe --install.
60 changes: 60 additions & 0 deletions sysdistrowt.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
$wtPath = Get-ChildItem "$env:LOCALAPPDATA\Packages\" -Directory | Where-Object { $_.Name -like "Microsoft.WindowsTerminal_*" } | Select-Object -ExpandProperty FullName
$wtpPath = Get-ChildItem "$env:LOCALAPPDATA\Packages\" -Directory | Where-Object { $_.Name -like "Microsoft.WindowsTerminalPreview_*" } | Select-Object -ExpandProperty FullName

if (!$wtPath -and !$wtpPath) {
Write-Host "Windows Terminal is not installed."
exit
}

$wslProfile = @{
name = "WSL System Distro"
commandline = "wsl.exe -u root --system"
hidden = $false
guid = [guid]::NewGuid().ToString("B")
icon = "C:\Windows\System32\wsl.exe"
startingDirectory = ""
}

if ($wtPath) {
Write-Host "Windows Terminal is installed."
if (Test-Path "$wtPath\LocalState\settings.json") {
Write-Host "Windows Terminal settings.json found."
} else {
Write-Host "Windows Terminal settings.json not found, creating..."
$wtExePath = Get-ChildItem "$env:ProgramFiles\WindowsApps\Microsoft.WindowsTerminal_1*" -Directory | Select-Object -ExpandProperty FullName
$mywtProcess = Get-Process -Name "WindowsTerminal"
$mywtPid = $mywtProcess.Id
Start-Process "$wtExePath\wt.exe"
Start-Sleep -Seconds 1
Get-Process -Name "WindowsTerminal" | Where-Object { $_.Id -ne $mywtPid } | Stop-Process
}
$wtSettings = Get-Content "$wtPath\LocalState\settings.json" -Raw | ConvertFrom-Json
if ($wtSettings.profiles.list | Where-Object { $_.Name -eq $wslProfile.Name }) {
$wtSettings.profiles.list = $wtSettings.profiles.list | Where-Object { $_.Name -ne $wslProfile.Name }
}
$wtSettings.profiles.list += $wslProfile
$wtSettings | ConvertTo-Json -Depth 100 | Set-Content "$wtPath\LocalState\settings.json"
Write-Host "Windows Terminal settings.json updated."
}

if ($wtpPath) {
Write-Host "Windows Terminal Preview is not installed."
if (Test-Path "$wtpPath\LocalState\settings.json") {
Write-Host "Windows Terminal Preview settings.json found."
} else {
Write-Host "Windows Terminal Preview settings.json not found, creating..."
$wtpExePath = Get-ChildItem "$env:ProgramFiles\WindowsApps\Microsoft.WindowsTerminalPreview_1*" -Directory | Select-Object -ExpandProperty FullName
$mywtProcess = Get-Process -Name "WindowsTerminal"
$mywtPid = $mywtProcess.Id
Start-Process "$wtpExePath\wt.exe"
Start-Sleep -Seconds 1
Get-Process -Name "WindowsTerminal" | Where-Object { $_.Id -ne $mywtPid } | Stop-Process
}
$wtpSettings = Get-Content "$wtpPath\LocalState\settings.json" -Raw | ConvertFrom-Json
if ($wtpSettings.profiles.list | Where-Object { $_.Name -eq $wslProfile.Name }) {
$wtpSettings.profiles.list = $wtpSettings.profiles.list | Where-Object { $_.Name -ne $wslProfile.Name }
}
$wtpSettings.profiles.list += $wslProfile
$wtpSettings | ConvertTo-Json -Depth 100 | Set-Content "$wtpPath\LocalState\settings.json"
Write-Host "Windows Terminal Preview settings.json updated."
}
61 changes: 61 additions & 0 deletions wsl-reset.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Error "This script must be run as an administrator."
Exit 1
}


if (-not $args) {
Write-Error "This script must be run with either --reset, --hard-reset, or --destructive-reset."
Exit 1
}


$arg = $args[0]
switch ($arg) {
"--reset" {
wsl.exe --shutdown -ErrorAction SilentlyContinue 2>$null
Restart-Service "Windows Subsystem for Linux" -Force -ErrorAction SilentlyContinue
if ($LASTEXITCODE -ne 0) {
Restart-Service LxssManager -Force -ErrorAction SilentlyContinue
}
wsl.exe --update > $null
Write-Host "WSL has been shutdown, Windows service restarted, and updated, if applicable."
}
"--hard-reset" {
wsl.exe --shutdown -ErrorAction SilentlyContinue 2>$null
Stop-Service "Windows Subsystem for Linux" -Force -ErrorAction SilentlyContinue
if ($LASTEXITCODE -ne 0) {
Stop-Service LxssManager -Force -ErrorAction SilentlyContinue
}
if (-not (Get-AppxPackage *WindowsSubsystemForLinux* | Remove-AppxPackage -ErrorAction SilentlyContinue 2>$null)) {
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -ErrorAction SilentlyContinue 2>$null
}
wsl.exe --install --no-launch
Write-Host "WSL has been shutdown and re-installed."
}
"--destructive-reset" {
wsl.exe --shutdown -ErrorAction SilentlyContinue 2>$null
Restart-Service "Windows Subsystem for Linux" -Force -ErrorAction SilentlyContinue
if ($LASTEXITCODE -ne 0) {
Restart-Service LxssManager -Force -ErrorAction SilentlyContinue
}
$distros = Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | ForEach-Object { $_.GetValue("DistributionName") }
foreach ($distro in $distros) {
wsl.exe --unregister $distro -ErrorAction SilentlyContinue 2>$null
$folder = Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs\$distro"
if (Test-Path $folder) {
Remove-Item $folder -Recurse -Force
}
}
if (-not (Get-AppxPackage *WindowsSubsystemForLinux* | Remove-AppxPackage -ErrorAction SilentlyContinue 2>$null)) {
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -ErrorAction SilentlyContinue 2>$null
}
wsl.exe --install --no-launch
Write-Host "WSL has been shutdown, all distros unregistered, and WSL has been re-installed."
}
default {
Write-Error "Invalid argument. This script must be run with either --reset, --hard-reset, or --destructive-reset."
Exit 1
}
}

0 comments on commit 211cf75

Please sign in to comment.