If you have Windows Script Host installed and enabled, you can do this (sorry, I've only had the time to test .bat files):
<% set wshell = CreateObject("WScript.Shell") wshell.run "c:\file.bat" set wshell = nothing %> |
Where c:\file.bat is something like:
net stop iisadmin /y net start w3svc
|
If you do not have the ability to use WSH, you can use
ASPExec or
DynuExec.
If you are trying to retrieve output from a command, see the example in
Article #2033, which captures the results of a PING command. Another alternative is to pipe the output to a text file, which you can then read using the FileSystemObject; for example:
<% set wshell = CreateObject("WScript.Shell") wshell.run "%COMSPEC% /C dir c:\ > c:\dir.txt", 0, TRUE set wshell = nothing set fso = CreateObject("Scripting.FileSystemObject") set fs = fso.openTextFile("c:\dir.txt", 1, TRUE) response.write replace(replace(fs.readall,"<","<"),vbCrLf,"<br>") fs.close: set fs = nothing: set fso = nothing %> |
If you receive the following error:
Error Type: (0x80070002) /<file>.asp, line <line> |
This usually means that the file is not found. Check the name and path of the file you passed to the run method.
If you receive the following error:
Microsoft VBScript runtime error '800a0046' Permission denied /<file>.asp, line <line> |
Then either one of two things happened. Either the interactive user (whether IUSR_MachineName or an authenticated user) does not have permissions to the file or folder being executed; or, the interactive user does not have permissions to one or more of the commands being called within the file. Don't get caught up in the common assumption that IUSR_machineName is included in the "Everyone" group... and don't just give "Everyone" full access to this folder, or put IUSR in the Administrators group. You have to explicitly give IUSR permissions to file, folder, and anything else that the command has to touch.
If you are running IIS 6.0, you might also want to check out
KB #311481.
One further word of warning: do not run any command that raises a prompt, dialog, msgbox or any other GUI. This will not run from ASP and could hang your entire system (since the invisible GUI will be waiting for a reply from some fictional person sitting at the terminal of the server).