From 9fdce8a4d2fcf9877eed6f819a25c177aa29b289 Mon Sep 17 00:00:00 2001 From: Hayden Barnes Date: Thu, 12 Oct 2023 12:19:15 -0400 Subject: [PATCH] Fix argument parsing in wsl-reset binary Fixes #9 --- README.md | 8 ++++---- wsl-reset.ps1 | 23 ++++++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 928442b..bbf213f 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,11 @@ To run wsl-dist-update as a service, copy wsl-dist-update.exe to a permanent loc A troubleshooting utility that resets the WSL 2 stack to various degrees. -`wsl-reset --reset` - Shuts down WSL, resets the WSL service, and installs any WSL updates, if available. +`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 -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. +`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 @@ -51,4 +51,4 @@ Adds the WSL System Distro (CBL-Mariner) to the Windows Terminal and/or Windows ## build-wslinternals -Builds wslinternals PowerShell scripts to .exe files using ps2exe. Requires PowerShell 5. +Builds wslinternals PowerShell scripts to .exe files using ps2exe. Must be run as Administrator on PowerShell 7 or on PowerShell 5. diff --git a/wsl-reset.ps1 b/wsl-reset.ps1 index bcf2342..77d8204 100644 --- a/wsl-reset.ps1 +++ b/wsl-reset.ps1 @@ -1,19 +1,16 @@ +param ( + [switch]$reset, + [switch]$hardReset, + [switch]$destructiveReset +) 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" { +switch ($true) { + $reset { wsl.exe --shutdown -ErrorAction SilentlyContinue 2>$null Restart-Service "Windows Subsystem for Linux" -Force -ErrorAction SilentlyContinue if ($LASTEXITCODE -ne 0) { @@ -22,7 +19,7 @@ switch ($arg) { wsl.exe --update > $null Write-Host "WSL has been shutdown, Windows service restarted, and updated, if applicable." } - "--hard-reset" { + $hardReset { wsl.exe --shutdown -ErrorAction SilentlyContinue 2>$null Stop-Service "Windows Subsystem for Linux" -Force -ErrorAction SilentlyContinue if ($LASTEXITCODE -ne 0) { @@ -34,7 +31,7 @@ switch ($arg) { wsl.exe --install --no-launch --no-distribution Write-Host "WSL has been shutdown and re-installed." } - "--destructive-reset" { + $destructiveReset { wsl.exe --shutdown -ErrorAction SilentlyContinue 2>$null Restart-Service "Windows Subsystem for Linux" -Force -ErrorAction SilentlyContinue if ($LASTEXITCODE -ne 0) { @@ -55,7 +52,7 @@ switch ($arg) { 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." + Write-Error "This script must be run with either --reset, --hard-reset, or --destructive-reset." Exit 1 } } \ No newline at end of file