Tanmaya Thopate, Chincholi Morachi, Low cost WEBSITE development in Pune, Windows Tips n Tricks

Wednesday, September 17, 2008

Execute a batch file in ASP

How do I execute a batch file in ASP?

On the Web hosting Server , one needs WSH, ASPExec or DynuExec available on the Hosting Server to execute the scripts which tries to execute batch files.

For example,

1) To start w3svc.
----------------------------------------------------------------------------------------------
<%
set wshell = CreateObject("WScript.Shell") wshell.run "c:\execute.bat"
set wshell = nothing
%>
----------------------------------------------------------------------------------------------

This bat file contains
----------------------------------------------------------------------------------------------
net stop iisadmin /y
net start w3svc
----------------------------------------------------------------------------------------------

2) Ping response in your ASP code.
----------------------------------------------------------------------------------------------
<% Response.Buffer = true %>
<%
url = "www.host.co.in"
Set objWShell = CreateObject("WScript.Shell")
Set objCmd = objWShell.Exec("ping " & url)
strPResult = objCmd.StdOut.Readall()
set objCmd = nothing: Set objWShell = nothing

strStatus = "offline"
if InStr(strPResult,"TTL=")>0 then strStatus = "online"

response.write url & " is " & strStatus
response.write ".
" & replace(strPResult,vbCrLf,"
")

%>
----------------------------------------------------------------------------------------------

Some time, you may come across errors if code is improrely configured:
1)
----------------------------------------------------------------------------------------------
Error Type:
(0x80070002)
/.asp, line
----------------------------------------------------------------------------------------------

it means that the file is not found, you will need to check the name and path of the file

2)
----------------------------------------------------------------------------------------------
Microsoft VBScript runtime error '800a0046'
Permission denied
/.asp, line
----------------------------------------------------------------------------------------------

Either IUSR_MachineName or an authenticated user does not have permissions to the file or folder or the IUSR does not have permissions to one or more of the commands being called within the file. Never assume that IUSR_machineName is included in the "Everyone" group... and never 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.

And one more thing, never include such script which needs users interaction, because this code runs in background and may be waiting for response from the User. This may lead to Web hosting Server hang.

No comments:

 
Add to Technorati Favorites