Process.Start cmd.exe不会运行在IIS中运行时作为文件传递的cmd文件 [英] Process.Start cmd.exe won't run cmd file that is passed as agument when running in IIS

查看:182
本文介绍了Process.Start cmd.exe不会运行在IIS中运行时作为文件传递的cmd文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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文件c:\mypath\myfile.cmd的内容包括: / p>

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

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

此cmd文件以预期的方式运行,生成myfilelog.txt并返回测试行2.当使用c#代码执行时:

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



  • 选项2按照预期失败,表明myfile_badname.cmd未被识别为有效的命令

  • 选项3按预期工作 - 它返回myfile.cmd的内容作为响应 - 这确认了我可以找到并读取该文件。

  • 选项4不工作 - 数字,它应该。它不挂断,也不返回任何响应,并且不执行cmd文件(没有myfilelog.txt产生)。

  • 选项5 - 与选项4相同的结果。

  • 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.

注意 - 我也试过修改myfile.cmd来删除第1行(创建日志文件)以回应响应。以防万一它是创建日志文件的权限问题。相同的结果。

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.

任何帮助将不胜感激。

更新为添加解决方案:

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

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.

要运行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运行的用户提供多少设置和权限或任何类型的配置更改,它都将永远不会工作。

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将无法正常工作

这篇关于Process.Start cmd.exe不会运行在IIS中运行时作为文件传递的cmd文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆