Run Powershell script from URL without temporary file

powershell

Solution

You can convert content to script block and then invoke it as command with arguments

$Script = Invoke-WebRequest 'http://example.com/powershell.ps1'
$ScriptBlock = [Scriptblock]::Create($Script.Content)
Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList ($args + @('someargument'))

Problem

I want to write a PowerShell script that runs a PowerShell script stored at a URL, passing both custom arguments and forwarding any arguments that are given to this PowerShell. Concretely, I can do: ``` $VAR=Invoke-WebRequest http://example.com/powershell.ps1 $TEMP=New-TemporaryFile $FILE=$TEMP.FullName + ".ps1" $VAR.Content | Out-File $FILE & $FILE somearguments $args ``` I'd ideally like to do that without using a temporary file, however `powershell -content -` doesn't seem to allow also passing arguments. Is there any way to avoid the temporary file?

Original source