Process.Start cmd.exe won't run cmd file that is passed as agument when running in IIS

你离开我真会死。 提交于 2019-12-11 03:12:39

问题


I've been searching and experimenting all morning with this one and I'm stumped. I have an aspx page running in IIS and calling the following c# function. I'm trying to have it run a cmd file and return the output from the cmd file. I've experimented with the five different options in the code below:

protected String RunMyCmdFileAndGetResponse() {
    Process proc = new Process ();
    proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
//  proc.StartInfo.Arguments = @"/c echo hello";                    <== 1
//  proc.StartInfo.Arguments = @"/c c:\mypath\myfile_badname.cmd";  <== 2
//  proc.StartInfo.Arguments = @"/c type c:\mypath\myfile.cmd";     <== 3
    proc.StartInfo.Arguments = @"/c c:\mypath\myfile.cmd";      //  <== 4
//  proc.StartInfo.Arguments = @"/c call c:\mypath\myfile.cmd";     <== 5
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.CreateNoWindow = true;

    proc.Start();
    string response = proc.StandardOutput.ReadToEnd();
    response += proc.StandardError.ReadToEnd();
    proc.WaitForExit();
    return response;
}

Cmd file c:\mypath\myfile.cmd contents are:

@echo test line 1 > c:\mypath\myfilelog.txt
@echo test line 2

This cmd file works as expected when run manually, producing myfilelog.txt and returning test line 2. When executed with the c# code:

  • Option 1 works - returns 'hello' response as expected.
  • Option 2 fails as expected, indicating myfile_badname.cmd is not recognized as a valid command
  • Option 3 works as expected - it returns the contents of myfile.cmd as the response - this confirms I am able to find and read the file.
  • Option 4 does not work - as near as I can figure, it should. It does not hang up, but also does not return any response at all, and does not execute the cmd file (no myfilelog.txt produced).
  • Option 5 - same results as option 4.

Note - I've also tried modifying myfile.cmd to remove line 1 (creating the log file) and only leave line 2 to echo a response. Just in case it's a permission issue creating the log file. Same result.

Any help would be appreciated!

Updated to add solution:

The answer from @MaxOvrdrv got me thinking a different way. There does indeed appear to be some kind of limitation when running Process.Start within an IIS context with UseShellExecute = false - if the primary argument is an executable file (cmd file, script file, etc), it will not run it. I tried passing SomeExample.cmd to cmd.exe, and SomeExample.js to cscript.exe.

However... I was able to trick it with a level of indirection, such that the executable file name is no longer the first argument, and it works just fine that way.

To run a cmd file:

string theResponse = RunMyCmdAndGetResponse(@"c:\somepath\mycmd.cmd");

protected String RunMyCmdAndGetResponse(string cmdPath) {
    Process proc = new Process ();
    proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
    proc.StartInfo.Arguments = "/c cmd /c " + cmdPath;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.CreateNoWindow = true;

    proc.Start();
    proc.WaitForExit();
    string response = proc.StandardOutput.ReadToEnd();
    response += proc.StandardError.ReadToEnd();
    return response;
}

To run a script file:

string theResponse = RunMyScriptAndGetResponse(@"c:\somepath\myscript.js");

protected String RunMyScriptAndGetResponse(string scriptPath) {
    Process proc = new Process ();
    proc.StartInfo.FileName = @"c:\Windows\System32\cmd.exe";
    proc.StartInfo.Arguments = "/c cscript //nologo " + scriptPath;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.CreateNoWindow = true;

    proc.Start();
    proc.WaitForExit();
    string response = proc.StandardOutput.ReadToEnd();
    response += proc.StandardError.ReadToEnd();
    return response;
}

回答1:


Running a batch file or any process per say from an ASPX page is futile as IIS does not run under a true Windows User Context. Because of this, regardless of how many settings and rights you give to the user that the AppPool is running under or whatever type of config changes you make, it simply will never work. I ran into the same problem quite a while back and basically it's impossible.

See my previous questions (and comments), along with accepted answer for possible "conceptual" solution to your current problem, here:

Process.Start won't work




回答2:


What about not using "cmd"?

Process proc = new Process ();
proc.StartInfo.FileName = @"c:\mypath\myfile.cmd";


来源:https://stackoverflow.com/questions/28590924/process-start-cmd-exe-wont-run-cmd-file-that-is-passed-as-agument-when-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!