Process.Start cmd.exe 在 IIS 中运行时不会运行作为参数传递的 cmd 文件

2024-04-01

我整个早上都在寻找和试验这个,但我被难住了。我有一个在 IIS 中运行的 aspx 页面并调用以下 c# 函数。我试图让它运行 cmd 文件并返回 cmd 文件的输出。我在下面的代码中尝试了五个不同的选项:

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 文件 c:\mypath\myfile.cmd 内容为:

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

手动运行时,此 cmd 文件按预期工作,生成 myfilelog.txt 并返回测试行 2。使用 c# 代码执行时:

  • 选项 1 有效 - 按预期返回“hello”响应。
  • 选项 2 按预期失败,表明 myfile_badname.cmd 未被识别为有效命令
  • 选项 3 按预期工作 - 它返回 myfile.cmd 的内容作为响应 - 这确认我能够找到并读取该文件。
  • 选项 4 不起作用 - 据我所知,它应该起作用。它不会挂起,但也根本不返回任何响应,并且不执行cmd文件(没有生成myfilelog.txt)。
  • 选项 5 - 与选项 4 的结果相同。

注意 - 我还尝试修改 myfile.cmd 以删除第 1 行(创建日志文件)并仅保留第 2 行以回显响应。以防万一创建日志文件时出现权限问题。相同的结果。

任何帮助,将不胜感激!

更新添加解决方案:

@MaxOvrdrv 的答案让我有了不同的想法。在 UseShellExecute = false 的 IIS 上下文中运行 Process.Start 时,确实似乎存在某种限制 - 如果主要参数是可执行文件(cmd 文件、脚本文件等),则它将不会运行它。我尝试将 SomeExample.cmd 传递给 cmd.exe,将 SomeExample.js 传递给 cscript.exe。

然而......我能够通过一定程度的间接来欺骗它,这样可执行文件名不再是第一个参数,并且这样就可以正常工作。

运行cmd文件:

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;
}

要运行脚本文件:

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;
}

从 ASPX 页面运行批处理文件或任何进程都是徒劳的,因为 IIS 不在真正的 Windows 用户上下文下运行。因此,无论您向运行 AppPool 的用户授予多少设置和权限,或者您进行任何类型的配置更改,它都永远不会起作用。我很久以前就遇到了同样的问题,基本上这是不可能的。

请参阅我之前的问题(和评论),以及针对当前问题的可能“概念”解决方案的已接受答案:

Process.Start 不起作用 https://stackoverflow.com/questions/26457757/process-start-wont-work

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Process.Start cmd.exe 在 IIS 中运行时不会运行作为参数传递的 cmd 文件 的相关文章