我通常有这个命令来运行一些powershell脚本:
& ".\NuGet\NuGet Package and Publish.ps1" -Version $env:appveyor_build_version -NuGet "C:\Tools\NuGet\nuget.exe" -apiKey $env:apiKey
工作正常但脚本在我的服务器本地找到.
我希望这样说:用参数运行这个脚本等等......很好..但脚本位于GIST或某些GitHub公共仓库中.
这可能吗?
在linux世界中,这通常被称为"管道到bash"
这是一个安装厨师文档的厨师的例子
curl -L https://omnitruck.chef.io/install.sh | sudo bash
Run Code Online (Sandbox Code Playgroud)
使用PowerShell可以完成同样的事情
. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install
Run Code Online (Sandbox Code Playgroud)
iwr是简写Invoke-WebRequest并且iex简称Invoke-Expression
由于您特别询问了传递参数,以下是args的示例.
. { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install -channel current -project chefdk
Run Code Online (Sandbox Code Playgroud)
您可以查看powershell脚本,以便更清楚地了解它的工作原理.
基本上将您的powershell脚本作为github gist托管,然后在脚本中包装模块中的所有内容
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'
}
Run Code Online (Sandbox Code Playgroud)
最佳做法
"管道打击"是有争议的,因为如果你不小心的话,攻击者理论上可以拦截和替换你自己的脚本.
如果我正确理解了这个问题,这对我有用:
(new-object net.webclient).DownloadFile('https://gist.githubusercontent.com/AndrewSav/c4fb71ae1b379901ad90/raw/23f2d8d5fb8c9c50342ac431cc0360ce44465308/SO33205298','local.ps1')
./local.ps1 "parameter title"
Run Code Online (Sandbox Code Playgroud)
输出:
调用参数:参数标题
这是下载并执行这个要点:https://gist.github.com/AndrewSav/c4fb71ae1b379901ad90
| 归档时间: |
|
| 查看次数: |
8725 次 |
| 最近记录: |