Solved

Executing a batch file using ASP running Windows Server 2003

Posted on 2004-10-18
6
1,128 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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,…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

22 Experts available now in Live!

Get 1:1 Help Now