Solved

Executing a batch file using ASP running Windows Server 2003

Posted on 2004-10-18
6
1,126 Views
Last Modified: 2008-01-09
I am trying to create an ASP page that will cause a batch file to be executed on my Internet Server running Windows Server 2003. I am familiar with, and have attempted to use the following services:

Option 1:
set newobj=createobject("wscript.shell")
newobj.Run "cmd.exe /c C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat"

I have also tried a variation to the above that seems to be the only codeset that actually does something - but it just seems to hang forever, and the batch file is never run - but the variation is:
newobj.Run("cmd.exe /c C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat", 0, True)

Option 2:
Using ASPExec from www.serverobjects.com
Set Executor = Server.CreateObject("ASPExec.Execute")
Executor.Application = "cmd.exe /c"
Executor.Parameters = "C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat"
strResult = Executor.ExecuteDosApp

Option 3:
Using DynuExec from www.dynu.com
Set myexec = Server.Createobject("Dynu.Exec")
Response.Write(myexec.execute("C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat"))

All of the above code does absolutely nothing. ( With the exception of adding the ", 0, True" to Option 1 variation 2 above - and again - it just hangs ).

I have successfully used these services ( with the exception of Dynu ) on previous versions of Windows ( 2000, NT, etc. ).

I have even opened security much wider than I would like by setting full control permission to the following files:
unzip.bat
cmd.exe
aspexec.dll
dynuexec.dll

In fact, I have even given full permissions for the following users on each of the above files:
Internet Guest Account
Interactive
Launch IIS Process
IIS_WPG
Administrators
Everyone

The contents of my batch file executes perfectly using the Command Prompt Window logged in as Administrator or when double clicked from the explorer window, but when called within the ASP page - nothing - no error - no activity - no hang up - nothing. Result from Shell produces no visible result when called by Response.Write().

The contents of my batch file is as follows - although I seriously doubt that this is even being called due to the nature of the problem.
c:\progra~1\winzip\wzunzip -n C:\Inetpub\wwwroot\SBO\admin\BCI\BB2ANE4.upd c:\inetpub\wwwroot\sbo\admin\BCI\Unzipped\
c:\progra~1\winzip\wzunzip -n C:\Inetpub\wwwroot\SBO\admin\BCI\BE620.UPD c:\inetpub\wwwroot\sbo\admin\BCI\Unzipped\

Can anyone help?

Thanks in advance!
0
Comment
Question by:CyberQuestions
6 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 12344033
How about using Windows scheduler?
Check out this PAQ of earlier today:
 http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21172502.html

-tushar
0
 

Author Comment

by:CyberQuestions
ID: 12344080
Thanks for the fast reply tushar.

I have though about that or just running the batch using a Windows Scheduler and for some applications this is a decent workaround - however, this is intermittently timed and when requested, need to display the results of the batch file.
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 12348300
Run a .BAT file from ASP.NET

Okay, running .BAT files from ASP.NET using  the System.Diagnostics.Process object and static methods this should be easy, right?  Well, this might work for you, but it certainly won't work on my machines. And after doing lots of reasearh on the issue, it seems that other people are also having problems with this too.  

I wrestled with permissions and all sorts of other stuff, trying to get a simple batch file to run, with no luck.  I tried lauching the bat file directly, launching cmd.exe and calling the bat file using stin. No dice.  It seems that something on my machine was keeping an unattended process from running bat files.  This makes sense, but I was never able to pinpoint what was preventing this, so I came up with a workaround.

I realized that since I could sucessfully run cmd.exe, and send commands to it via stin, I could just open the batch file, and send each line to cmd.exe, which is essentially the same as running a batch file itself.  This technique works great, and I thought I'd pass along the code here.

// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();

// Close the io Streams;
sIn.Close();
sOut.Close();

// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

http://dotnetjunkies.com/WebLog/bsblog/archive/2004/05/13/13484.aspx

Aeros
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now