Saturday, June 4, 2022

[SOLVED] Pasteable command to run a batch file from a URL

Issue

For some internal script distribution, I want to be able to give users a single command they can paste into a Windows cmd window that will run a batch script from a URL.

In bash, the equivalent would be eval "$(curl -sS https://example.com/my-script)" or curl https://example.com/my-script | bash (but really I do need something more like the former, because I want to set env vars in the existing cmd session).

Similarly, in PowerShell according to https://stackoverflow.com/a/68530475/68051 this could be iex (iwr https://example.com/my-script).Content.

What's the best equivalent cmd.exe command for doing this?

This is for trusted internal use, so I'm not very worried about security risks of running remote scripts. I'm looking for the quickest easiest command I can give people to paste that will work on a typical Windows machine with no special setup.


Solution

Try this:

powershell -Command Invoke-WebRequest -Uri https://example.com/my-script -OutFile %TEMP%\downloaded.bat && %TEMP%\downloaded.bat

It indeed uses powershell to get the file, then it executes it directly in current cmd.



Answered By - Wisblade
Answer Checked By - Terry (WPSolving Volunteer)