I've written a software that customers will install on Windows Server, and I wrote a HTML page for installation guide. How do I create a link on HTML page that will invoke my .bat script as an administrator. The .bat script will handle installation for customers. HTML page will be run as a file, that is without any server (including local host). The .bat script, html page, and binary will be in the same folder. The folder could be anywhere on the Windows server (I don't know where customers will place the folder).
3 Answers
One way of doing this would be to use a combination of AJAX and a script to accomplish this.
I'm going to assume the use of jQuery and PHP (just because they're so common) for this example.
jQuery just makes it shorter and this functionality will be available in most/all languages.
Let's say you have a page like this:
<html>
<body>
<a id="foo" href="#">Install</a>
<script src="install.js"></script>
</body>
</html>
And your javascript looks like this:
$("#foo").on("click", function(){
$.post('install.php');
});
In your install.php file, you could have this:
<?php
exec("mybatch.bat");
or
<?php
exec("cmd.exe /c test.bat");
However
Please note that what you are wanting to do has security problems.
Only do this if you are sure the environment is a secure one.
Running as administrator
This is a very difficult thing to do without cooperation from the client computer.
You more or less have to be running PHP or whatever language is installed locally as administrator.
Also, if running a batch file from a POST request is a bad idea, running a batch file as administrator is even worse. Avoid this.

- 2,624
- 21
- 38
-
He said that the NTML page doesn't have a back end server. It is just a static html document that the user is double clicking to open up. – JHixson Feb 12 '14 at 21:56
-
Running a .bat file from HTML alone is essentially impossible without finding MASSIVE browser vulnerabilities. I'm trying to give him options. If he needs to have software install work with no backend, he's going to have to code a proper installer. – JoshWillik Feb 12 '14 at 21:58
In internet explorer:
file://mydirectory/myfolder/mybatchfile.bat
Most browsers don't allow this functionality because it poses a huge security risk for a link on a web page to be able to run local files. Most of them have extensions that disable that security feature. I haven't tested any of them to see if they work, however. Even when this happens, however, the .bat run will happen like a download. The browser will "download" the .bat file, then give the user the opportunity to save it or run it. Unless your batch file is doing the administration elevation within the file itself, the user will have to manually run it as administrator.
Please keep in mind that these safeguards exist for a reason, and going around them is not a good idea. At the end of the day, however, there is no way to make the browser execute that code on its own. (Thankfully so!) There are better ways of accomplishing these kinds of tasks.
If you wanted to provide more details on what you are trying to accomplish, i'm sure we could come up with a more secure of doing what you want to do.

- 1,512
- 2
- 14
- 29
You can add in the html below anchor tag "a" with href="test.bat" Download the Bat file by clicking here ! this anchor tag will execute the test.bat file and after clicking on this window prompt to open/save/close if you click on open the batch file it will be executed
Save this bat file and Click Advanced under Shortcut and then Select the checkbox next to Run as administrator.

- 19
- 3