Creating parametrized PowerShell one-liners
Coming from unix world, I really enjoy so-called one-liners — easy to remember commands that do some useful bootstrapping.
Few examples from my dotfiles: https://github.com/voronenko/dotfiles
Saying, if I want to configure my favorite shell on some new VPS
or configure my dotfiles configuration on a more permanent box
That approach works pretty well on linux, thus when I have windows related work, I am trying to reuse similar approach. Few examples from my winfiles: script below configures my PowerShell profile on a new windows server, and optionally installs my “swiss knife” set of tools for the windows system.
Sometimes on Windows it is needed to additionally pre-configure bootstrap script. This article is actually note for myself how to do it quick next time :)
Challenge definition
Assume we have some bootstrap logic implemented in PowerShell, uploaded to some public location and we need one-liner for easier install. For purposes of the demo — that might be script, that installs some custom MSI artifact:
user can tune following script parameters:
Option A — almost manual “bootstrap.ps1 -param value”
Pros: actually nothing is needed, just works
Cons: harder to tune parameters programmatically
Validation — no overrides
Validation — with overrides
Acceptance: PASSED
Option B — X-Liner from pre-downloaded script
Put overrides only into $overrideParams , other will be picked up from default values in install script. Pros — you can detect and programmatically amend override parameters.
Acceptance: PASSED
Option C — X-Liner executing script from remote location
Put overrides only into $overrideParams , other will be picked up from default values in install script, downloaded from the remote location
Pros — you can detect and programmatically amend override parameters, bootstrap script can be located on your download location.
Acceptance: PASSED
Option D — Real one-liner using PowerShell module and iwr + iex
As stated, requiries installation logic packed as a PowerShell module (see bootstrap-module.ps1
)
. { iwr -useb https://path/to/bootsrap.ps1 } | iex; function -param value
Acceptance: PASSED
where bootstrap-module.ps1 is our original bootstrap file, but packed/wrapped into a module.
So far option D is the most one-linish :)
Check out https://github.com/Voronenko/ps_oneliners for examples from article.
We now have few approaches to choose from, to implement short “one-liners” to bootstrap some logic with PowerShell.