7

I'm trying to run a batch file on a server via an ASP.Net page, and it's driving my crazy. When I run the below code, nothing happnes - I can see from some log statements that this code runs, but the .bat file that I pass to the function never runs.

Could anybody please tell me what I'm doing wrong?

public void ExecuteCommand(string batchFileLocation)
{
   Process p = new Process();

   // Create secure password
   string prePassword = "myadminpwd";
   SecureString passwordSecure = new SecureString();
   char[] passwordChars = prePassword.ToCharArray();
   foreach (char c in passwordChars)
   {
       passwordSecure.AppendChar(c);
   }

   // Set up the parameters to the process
   p.StartInfo.FileName = @"C:\\Windows\\System32\cmd.exe";
   p.StartInfo.Arguments = @" /C " + batchFileLocation;
   p.StartInfo.LoadUserProfile = true;
   p.StartInfo.UserName = "admin";
   p.StartInfo.Password = passwordSecure;
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.CreateNoWindow = true;

   // Run the process and wait for it to complete
   p.Start();
   p.WaitForExit();
}

In the 'Application' Event Viewer log on the server, every time I try to run this, the following issue seems to occur:

Faulting application cmd.exe, version 6.0.6001.18000, time stamp 0x47918bde, faulting module kernel32.dll, version 6.0.6001.18000, time stamp 0x4791a7a6, exception code 0xc0000142, fault offset 0x00009cac, process id 0x8bc,application start time 0x01cc0a67825eda4b.

UPDATE

The following code works fine (it runs the batch file):

Process p = new Process();
p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = false;

// Run the process and wait for it to complete
p.Start();
p.WaitForExit();

This however doesn't (when i try to run as a specific user):

Process p = new Process();

// Create secure password
string prePassword = "adminpassword";
SecureString passwordSecure = new SecureString();
char[] passwordChars = prePassword.ToCharArray();
foreach (char c in passwordChars)
{
      passwordSecure.AppendChar(c);
}

p.StartInfo.FileName = batchFileLocation;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
p.StartInfo.UseShellExecute = false;
p.StartInfo.UserName = "admin";
p.StartInfo.Password = passwordSecure;

// Run the process and wait for it to complete
p.Start();
p.WaitForExit();
Himanshu
  • 31,810
  • 31
  • 111
  • 133
Jimmy Collins
  • 3,294
  • 5
  • 39
  • 57

2 Answers2

4

Just call the batch file directly:

p.StartInfo.FileName = batchFileLocation;

Also, make sure the WorkingDirectory is set to the right location:

p.StartInfo.WorkingDirectory= Path.GetDirectoryName(batchFileLocation);
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I've already tried this, I get the same results. The code seems to run OK, but the batch file never starts. – Jimmy Collins May 04 '11 at 14:34
  • @Jimmy - Have you checked the passed in parameter? Is it correct and the file is in the location passed in? – Oded May 04 '11 at 14:36
  • Yeah I've out put the parameter to a log file to check it's OK, and checked that the actual file exists. – Jimmy Collins May 04 '11 at 14:37
  • I've set this now also. Whatever code I use (using cmd.exe or callin the batch file directly), an error seems to occur - I've added it to my question - I see this under the 'Application' log in the Event Viewer on the server. – Jimmy Collins May 04 '11 at 14:47
  • @Jimmy - Is the site running under an account that has permissions to run this batch file and cmd.exe? – Oded May 04 '11 at 14:56
  • Yeah, the site is running under the same account as the one I'm trying to run the batch file from. It's an administritive account, and the only account on that machine. – Jimmy Collins May 04 '11 at 15:00
  • Hey Thanks it worked for me! the key is to add working directory and run the entire thing under Task.Run so that it will run in separate thread. When I added workingdirectory it didnot work form me then I run the whole process under Task.Run and it worked for me. – Rupesh Kumar Tiwari Oct 16 '15 at 17:47
1

A little google on "Faulting application cmd.exe" points me to this IIS forum.

It seems that you cannot create a new process in the background under IIS, unless you use the CreateProcessWithLogon method. (I have not tested this).

GvS
  • 52,015
  • 16
  • 101
  • 139
  • This looks like the issue I'm seeing alright - unfortunetely the link to the solution from the IIS forum thread seems to be down. – Jimmy Collins May 04 '11 at 15:43