I’m trying to run a batch file, as another user, from my web app. For some reason, the batch file hangs! I can see ‘cmd.exe’ running in the task manager, but it just sits there forever, unable to be killed, and the batch file is not running. Here’s my code:
SecureString password = new SecureString(); foreach (char c in 'mypassword'.ToCharArray()) password.AppendChar(c); ProcessStartInfo psi = new ProcessStartInfo(); psi.WorkingDirectory = @'c:\build'; psi.FileName = Environment.SystemDirectory + @'\cmd.exe'; psi.Arguments = '/q /c build.cmd'; psi.UseShellExecute = false; psi.UserName = 'builder'; psi.Password = password; Process.Start(psi);
If you didn’t guess, this batch file builds my application (a different application than the one that is executing this command).
The Process.Start(psi); line returns immediately, as it should, but the batch file just seems to hang, without executing. Any ideas?
EDIT: See my answer below for the contents of the batch file.
- The output.txt never gets created.
I added these lines:
psi.RedirectStandardOutput = true; Process p = Process.Start(psi); String outp = p.StandardOutput.ReadLine();
and stepped through them in debug mode. The code hangs on the ReadLine(). I’m stumped!
I believe I’ve found the answer. It seems that Microsoft, in all their infinite wisdom, has blocked batch files from being executed by IIS in Windows Server 2003. Brenden Tompkins has a work-around here:
http://codebetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx
That won’t work for me, because my batch file uses IF and GOTO, but it would definitely work for simple batch files.