In the linux world, this is commonly referred to as 'piping to bash'
Here is an example that installs chef taken from the chef documentation
curl -L https://omnitruck.chef.io/install.sh | sudo bash
The same thing can be done with powershell
. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install
iwr
is shorthand for Invoke-WebRequest
and iex
is short for Invoke-Expression
Since you specifically asked about passing in arguments, here is an example with args.
. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install -channel current -project chefdk
You can look at the powershell script for a clearer understanding how it works.
Basically host your powershell script as a github gist, then in the script wrap everything in a module
new-module -name foobar -scriptblock {
Function Foo() {
}
Function Bar() {
}
Function Install-Project() {
param (
[string]$project = 'chef',
[string]$channel = 'stable'
)
Foo
Bar
}
set-alias install -value Install-Project
export-modulemember -function 'Foo','Bar' -alias 'install'
}
Best practices
'Piping to bash' is controversial because it is theoretically possible for attacker to intercept and replace your script with their own if you aren't careful.
- Only install scripts you control or fully trust
- Use permalinks or verify hashes of file you download
- Only download from https