I have the following code in my project that installs linux sl
on Windows using PowerShell. Is there anything you'd improve about this?
param([String]$Profile, [Switch]$Help, [Switch]$Force)
$ErrorMessages = @()
$Payload = "
# <Code inserted by Install-Steam-Locomotive>
function Steam-Locomotive {wsl sl -e}
function Steam-Locomotive-Force {wsl sl}
# </>
"
$HelpText = "This script helps you use the tremendous `"sl`" program in Windows PowerShell.
Simply download the .ps1 file and execute it.
If the script finishes successfully, you can type Steam-Locomotive in PS to start the interruptable `"sl -e`".
Use Steam-Locomotive-Force to prevent interruption (`"sl`").
Example:
PS C:\Users\User\Downloads\> .\Install-Steam-Locomotive.ps1 -Profile $PROFILE
"
if ($Help) {
Write-Output $HelpText
exit
}
if (!(Get-Command -Name "wsl" -CommandType "Application" -ErrorAction SilentlyContinue)) {
$ErrorMessages += @("You don't have WSL installed. Cannot continue.")
}
if (!$Profile) {
$ErrorMessages += @("Please supply your profile location under -Profile. Cannot continue.")
if (Get-Content $Profile -ErrorAction SilentlyContinue | Select-String "Steam-Locomotive") {
$ErrorMessages += @("Your profile seems to already contain something called `"Steam-Locomotive`". Use -Force to continue anyways.")
}
}
if ((Get-Command -Name "Steam-Locomotive" -ErrorAction SilentlyContinue) -and !$Force) {
$ErrorMessages += @("It seems a command named `"Steam-Locomotive`" is already installed. Use -Force to continue anyways.")
}
if ($ErrorMessages) {
Write-Output $ErrorMessages
exit
}
if (!(wsl command -v sl)) {
Write-Output "First, install SL."
wsl sudo apt install sl
}
if (wsl command -v sl) {
Add-Content $Profile $Payload
Write-Output "Done!"
}
if (!(wsl command -v sl)) {
Write-Output "Failed installing wsl."
exit
}
Write-Output "Success!"
EDIT: I had an error in this script. The fixed version is up on the project page.
wsl
would be the Windows version ofsl
. Nice confusion. :) \$\endgroup\$sl
?Set-Location
is already built-in. Is that the bloody steam locomotive that shows up when usingls
by mistake in a Windows environment? I usually fix that by actually installingls
... \$\endgroup\$sl
in linux... if you have it installed. This is if you want the animation in PowerShell. \$\endgroup\$