我想编写一个PowerShell脚本,该脚本运行存储在URL中的PowerShell脚本,传递自定义参数并转发给予此PowerShell的任何参数。具体来说,我可以这样做:
$VAR=Invoke-WebRequest http://example.com/powershell.ps1
$TEMP=New-TemporaryFile
$FILE=$TEMP.FullName + ".ps1"
$VAR.Content | Out-File $FILE
& $FILE somearguments $args
我理想情况下不使用临时文件就这样做,但是powershell -content -
似乎也不允许传递参数。有没有办法避免临时文件?
答案 0 :(得分:7)
直接从chocolatey的网站上偷走了。我经常使用它比
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
答案 1 :(得分:5)
您可以将内容转换为脚本块,然后将其作为带参数的命令
调用$Script = Invoke-WebRequest 'http://example.com/powershell.ps1'
$ScriptBlock = [Scriptblock]::Create($Script.Content)
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList ($args + @('someargument'))
答案 2 :(得分:2)
此单行将从 URL 下载并获取文件
. ([Scriptblock]::Create((([System.Text.Encoding]::ASCII).getString((Invoke-WebRequest -Uri "${FUNCTIONS_URI}").Content))))
示例$FUNCTIONS_URI = "https://github.com/aem-design/aemdesign-docker/releases/latest/download/functions.ps1"
答案 3 :(得分:0)