5

So I'm putting together a batch file to run at startup that executes a small number of processes, one of them is to execute a reboot of a certain program at the end of the set of processes. I've been searching ways how to do this in the command line in windows but I need to be able to do this without opening a browser. What I need is to execute the reboot in the following url without opening a browser at all.

http://192.168.1.100/cgi-bin/reboot

Everything that I've tried has opened a new browser window. I don't want to have to download anything to get this going in windows if that's possible. Thanks for any suggestions.

Destroconut
  • 67
  • 1
  • 2
  • 8

4 Answers4

5

You know how sometimes the answer to the question you ask is not necessarily the answer you need? I have a vague suspicion this might be one of those times.

If this is a Windows machine you're trying to reboot, you can reboot it remotely without needing to use a CGI script served by the remote host. If the account you're logged in with on the triggering PC also has privileges on the remote PC, you can trigger the reboot with the shutdown command.

shutdown /m \\remotePC /r /t 0

Do shutdown /? in a cmd console for more info. Or if you must authenticate, you can use wmic instead.

wmic /node:remotePC /user:remotePCadmin /password:remotePCpass process call create "shutdown -r -t 0"

In case I was mistaken, here's the answer to the question you asked. The fastest way to execute a remote CGI script would be to use an XMLHTTPRequest with Windows Script Host (VBScript or JScript). Here's an example.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "URL=http://192.168.1.100/cgi-bin/reboot"
cscript /nologo /e:jscript "%~f0" "%URL%"

goto :EOF
@end // end batch / begin JScript chimera

var x = WSH.CreateObject("Microsoft.XMLHTTP");

x.open("GET",WSH.Arguments(0),true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState != 4) WSH.Sleep(50);

For what it's worth, you can also parse x.responseText as needed. You can scrape it as flat text, or even evaluate it as a hierarchical DOM object. This answer demonstrates such parsing. And if you just can't get enough, here are more examples.


If you'd rather have something simpler at the expense of efficiency, you can invoke a PowerShell command.

@echo off & setlocal

set "URL=http://192.168.1.100/cgi-bin/reboot"
powershell "ipmo BitsTransfer; Start-BitsTransfer \"%URL%\" \"%temp%\a\""
del /q "%temp%\a"

goto :EOF

You could probably alternatively use Invoke-WebRequest to avoid the temporary file, but Invoke-WebRequest requires PowerShell version 3 or newer. Start-BitsTransfer works with PowerShell version 2, so it should work on more computers. One might also use the [System.Net]::WebRequest .NET class, but it gets a little complicated constructing all the objects needed to proceed beyond fetching the HTTP headers to having the web server serve the web page. If you're curious, it looks something like this:

powershell "[void](new-object IO.StreamReader([Net.WebRequest]::Create(\"%URL%\").GetResponse().GetResponseStream())).ReadToEnd()"

Not exactly what I'd call simple. In hybrid format for easier readability:

<# : batch portion
@echo off & setlocal

set "URL=http://192.168.1.100/cgi-bin/reboot"
powershell -noprofile "iex (${%~f0} | out-string)"

goto :EOF

: end batch / begin PowerShell hybrid chimera #>

$request = [Net.WebRequest]::Create($env:URL)
$response = $request.GetResponse()
$stream = $response.GetResponseStream()
$reader = new-object IO.StreamReader($stream)
[void]$reader.ReadToEnd()

In any case, any PowerShell solution will be a second or two slower than the JScript solution near the top of this answer. powershell.exe takes a second or two to load (indeed, several seconds if it's not been loaded since Windows was last rebooted); whereas cscript.exe fires nearly instantly.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Thanks for the detailed response. The machine that's being rebooted is not a windows machine, I guess I should have said that in the orginal question. I'm going to try out your second and third solution and see which one works best for what I'm looking for. – Destroconut May 01 '16 at 15:28
  • @Destroconut what's the verdict? – rojo May 02 '16 at 16:22
  • when I try to run the powershell script the powershell app window opens up then closes very quickly but the desired outcome is not there. Also, it opens so quickly I can read the text to see what's going on. I may just have to send curl out to all the boxes... didn't want to have to do that. – Destroconut May 03 '16 at 13:56
  • @Destroconut if you want to see the output of a script, don't double-click it to execute it. – rojo May 03 '16 at 14:12
  • executed your powershell script broken into portions and it worked perfectly. Thanks so much – Destroconut May 03 '16 at 14:23
4

If you cannot download anything, then Windows PowerShell is the best option. You can call it from a PS script file, or directly from the command line in a batch file:

powershell.exe -noprofile -command "Invoke-WebRequest -Uri http://192.168.1.100/cgi-bin/reboot"

You can also consider Curl for that type of process. There is no equivalent in Windows, so it will require a download.

curl http://192.168.1.100/cgi-bin/reboot

Curl will not open a browser window, and has great command line options (See curl -help), and will return error codes for batch file error handling.

Rodrigo Murillo
  • 13,080
  • 2
  • 29
  • 50
  • Thanks for the suggestion, I will try it and let you now how it turned out. – Destroconut Apr 30 '16 at 00:28
  • I would really like to find a way to accomplish this without have any additional downloads as I'm going to have to place this on multiple machines that I may not be able to download to. – Destroconut Apr 30 '16 at 00:36
  • `Invoke-WebRequest` will work fine for Windows 10 (and maybe 8.x?), but every Windows 7 machine I've used defaults to PowerShell version 2 when invoked from a batch script. – rojo Apr 30 '16 at 02:09
  • You can/should upgrade PowerShell when you can on those machines, if possible. I tested this on Windows 7/PS3. If you are stuck with PowerShell 2 see http://stackoverflow.com/questions/25120703/ – Rodrigo Murillo Apr 30 '16 at 14:11
1

I don't think Windows comes with that feature.

Try making a VBscript that will open the browser Windows but will not display anything maybe.

Eli Richardson
  • 934
  • 7
  • 25
1

To process URL response direct, from Win CMD:

powershell "Invoke-WebRequest -Uri <url>"

To save server response to file:

powershell "Invoke-WebRequest -Uri <url> -outfile <file.ext>"

.NET solution; from Win CMD:

powershell "[System.Net.Webclient]::new().DownloadString(\"<URL>\")"
Zimba
  • 2,854
  • 18
  • 26