June 22, 2007

Shell commands from classical ASP

If for some reason your boss think its a good idea to make shell calls from ASP, this web page may help you, regardless of whether or not you feel compelled to agree to his ridiculous demands. Certainly there are right a proper uses for this technology but we are not here to judge, only to code.

Its actually quite simple. Three easy lines to remember. First we create the object. The object we want to create is the scripting object called wscript.shell.


set wshell = server.createobject("wscript.shell")



The next line is the command we want to run. Let's not worry ourselves with silly scripts and what have you. Put whatever you want to execute in an EXE or BAT and call it.


intReturn = wshell.run("c:\myCommand.exe")


Lastly a good programmer releases whatever he/she has taken.


set wshell = nothing


Here are the 5 important notes which you must know:


  1. If you enclose your asp scripting with anything other than <% ... %> you may be in trouble.

  2. The process runs under IWAM_MachineName. If your computer is called ServerA, That user is IWAM_ServerA. Thus you must give that user execute rights to your executing exe which is myCommand.exe and other other resource it uses such as folders and log files.

  3. IWAM_MachineName is hardly ever logged on interactively (like never) so don't expect code such as MsgBox("Hello world") or running notepad.exe to work. It won't!

  4. The exe runs asynchronously. Which means line 2 returns before the application has finished running. Probably there's a way for the thread to wait but I can't be bothered about that now.

  5. Running shells in a webserver is .... not advisable. Don't try it unless you know what you're doing and even if you are I'm not responsible if your webserver gets hacked.



Thanks and goodnight

No comments: