3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ When I read the code, I thought that wsl would be the Windows version of sl. Nice confusion. :) \$\endgroup\$ – Roland Illig Feb 20 '20 at 21:19
  • \$\begingroup\$ Who or what is sl? Set-Location is already built-in. Is that the bloody steam locomotive that shows up when using ls by mistake in a Windows environment? I usually fix that by actually installing ls... \$\endgroup\$ – Mast Feb 20 '20 at 21:27
  • 1
    \$\begingroup\$ I'd recommend you edit the title to describe/summarize the purpose of the code, as strongly suggested by the watermark in the title textbox. As it stands, any powershell question could have that title - and without "powershell" in the title, literally any on-topic post on this site could have that title. \$\endgroup\$ – Mathieu Guindon Feb 20 '20 at 21:28
  • \$\begingroup\$ @Mast It's the locomotive that shows up when you type sl in linux... if you have it installed. This is if you want the animation in PowerShell. \$\endgroup\$ – schuelermine Feb 21 '20 at 15:20
2
\$\begingroup\$
  • Use the script template for documentation
  • Use a cmdlet binding for parameter validation and help text
  • Personally, I run with $ErrorActionPreference = "Stop"
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.