1

If I have a Powershell script accessible through HTTP, how can I download it and execute it on the fly like what we can do in the bash?

curl --user user:pass https://fake.url | bash

2 Answers 2

2

First of all, you shouldn't do that. I mean, downloading random code from the web and running it without taking a look at it? Yikes!

Yes, it's convenient, but it's also a horribly stupid idea, just as curl | bash, or even worse, curl | sudo bash is.

In most cases you can probably use WebClient.DownloadString and pass it to Invoke-Expression: Invoke-Expression (New-Object Net.WebClient).DownloadString(«url»). I'm not sure whether that still opens a script: scope for variables, though, so if that scope is used in the script it might not work. In that case you can download it as a file and execute the script. Then you also have to take care of the execution policy, though.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reminding me download and executing random script is a dumb idea. What I am actually going to do is download script from an internal central repository which script is verified already. And I successfully run following command: Invoke-RestMethod -Uri https://fake.url -Method Get |Invoke-Expression
It seems to be a popular way to install PowerShell modules these days. WMF5 apparently brings an Install-Module that can download modules from a central repository (the PowerShell Gallery) which means that once everyone is on PowerShell 5 (might just take a while) we can let those things die for most cases.
If this isnt being run interactively make sure to check for errors etc. I would dump the script block to stdout to help with troubleshooting down the line #murphyslaw
1

Try

curl --user user:pass https://fake.url | powershell -Command -

If the value of Command is "-", the command text is read from standard input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.