I usually have this command to run some powershell script:
& ".\NuGet\NuGet Package and Publish.ps1" -Version $env:appveyor_build_version -NuGet "C:\Tools\NuGet\nuget.exe" -apiKey $env:apiKey
works fine but the script is found locally on my server.
I'm hoping to say: run this script with arguments, etc.. fine .. but the script is located as a GIST or in some GitHub public repo.
Is this possible?
To make sure PowerShell executes what you want, navigate in the command line to the same directory where you will save your scripts. Name the script Unnamed_Arguments_Example_1. ps1 and run it with the argument FOO. It will echo back FOO.
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.
If I understood the question correctly, this is what worked for me:
(new-object net.webclient).DownloadFile('https://gist.githubusercontent.com/AndrewSav/c4fb71ae1b379901ad90/raw/23f2d8d5fb8c9c50342ac431cc0360ce44465308/SO33205298','local.ps1')
./local.ps1 "parameter title"
Output:
Called with parameter: parameter title
This is downloading and executing this gist: https://gist.github.com/AndrewSav/c4fb71ae1b379901ad90
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With