Running a batch file from an ASP.Net page

I am trying to run a batch file on a server through an ASP.Net page, and it drives me crazy. When I run the code below, nothing happens - I can see from some log statements that this code works, but the .bat file that I pass to the function never runs.

Can someone please tell me what I am 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 View Applications log on the server, every time I try to start it, the following problem occurs:

Incorrect cmd.exe application, version 6.0.6001.18000, timestamp 0x47918bde, kernel32.dll error module, version 6.0.6001.18000, timestamp 0x4791a7a6, exception code 0xc0000142, error offset 0x00009cac, process ID 0x8bc, application start time 0x01cc0a67825ed4b.

UPDATE

The following code works fine (it runs a 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(); 

However, this is not the case (when I try to work 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(); 
+6
source share
2 answers

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

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

+1
source

Just call the batch file directly:

 p.StartInfo.FileName = batchFileLocation; 

Also, make sure that WorkingDirectory installed in the right place:

 p.StartInfo.WorkingDirectory= Path.GetDirectoryName(batchFileLocation); 
+4
source

Source: https://habr.com/ru/post/887284/


All Articles