0
votes

I am facing a predicament with running a bat file using the system.diagnostics.process object. I have the code for running a bat file in a class library which I compiled to a dll. I made the dll com interoperable by registering it using compile time. I signed it using strong key, exported the type library and put the dll into GAC using regasm and gacutil commands. I then created the object of the specific class in the dll that has the method for the bat file execution using the server.createobject method in vbscript. I then called the method for the bat execution. Method gets invoked alright but the cmd prompt is not popping up nor is the bat file being executed. I checked to see if its a problem with the interop dll but the dll worked fine with VB6 code. Can someone help me with this issue? I am not sure if its some permission issue on the IIS server. Or is cmd executions not possible via vbscript on ASP for dlls?

Thanks, Geo.

1
Does your batch invocation command include cmd.exe? I think, it should be something like cmd.exe /C yourscript.bat. - Andriy M
IIS 6/7 by default the DefaultAppPool runs as NETWORK SERVICE, IIS 7.5 runs as DefaultAppPool. learn.iis.net/page.aspx/624/application-pool-identities - ThatGuyInIT

1 Answers

0
votes

Apparently you may need to run cmd and then just inject input into it:

// 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();

http://codebetter.com/brendantompkins/2004/05/13/run-a-bat-file-from-asp-net/