Foo.cmd 不会在进程中输出行(在网站上) [英] Foo.cmd won't output lines in process (on website)

查看:15
本文介绍了Foo.cmd 不会在进程中输出行(在网站上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解 .NET 中 ProcessStartInfo 类的来龙去脉时遇到问题.我使用这个类来执行 .exe 程序,比如 FFmpeg,没有任何问题.

I've a problem understanding the in's and out's of the ProcessStartInfo class in .NET. I use this class for executing .exe programs like FFmpeg with no issues whatsoever.

但是当我使用 ProcessStartInfo 来启动一个 .cmd 程序时,就像一个只包含 @echo Hello world 的简单 foo.cmd,它不会输出任何东西.

But when I use ProcessStartInfo to start a .cmd program like a simple foo.cmd containing only @echo Hello world it doesn't output anything.

    ProcessStartInfo oInfo = new ProcessStartInfo(@"C:Program Files (x86)itmsfoo.cmd")
    {
        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    };

    using (Process p = new Process())
    {
        p.StartInfo = oInfo;
        p.OutputDataReceived += new DataReceivedEventHandler(transporter_OutputDataReceived);

        p.Start();

        p.BeginOutputReadLine();

        p.WaitForExit();
    }

private void transporter_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Response.Write(e.Data + " - line<br/>");
}

我看过很多例子,人们使用 cmd.exe 来启动 .cmd 程序,我也试过这个,但没有成功.程序只是无限期地加载.

I've seen a bunch of examples, where people use cmd.exe to start the .cmd program and I've tried this, but with no success. The program just keeps loading indefinitely.

    ProcessStartInfo oInfo = new ProcessStartInfo("cmd", "/c start foo.cmd")
    {
        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        WorkingDirectory = @"C:Program Files (x86)itms"
    };

在 Windows 和 Mac 上使用命令行工具时,foo.cmd 程序运行并成功输出.

The foo.cmd program works and outputs successfully when using a command line tool in Windows and on Mac.

谁能帮我揭开这个谜团.

Could someone please demystify this for me.

谢谢

编辑

代码在本地执行时行为正确.当我在我们的网站上执行代码时出现问题.要么程序不允许执行,要么输出以某种方式被禁用.

The code behaves correctly when executed locally. The problem arises when I execute the code on our website. Either the program isn't allowed to execute or the output is somehow disabled.

只有 cmd.exe 返回输出 ´"cmd", "/c dir"´ 例如返回有关当前文件夹内容的信息.

Only cmd.exe is returning output ´"cmd", "/c dir"´ is e.g. returning information about the current folder content.

这真的是权限问题吗?

推荐答案

我自己找到了答案,并将为任何感兴趣的人发布解决方案.

I found the answer myself and will post a solution for anyone interested.

问题的根源很难调试,因为问题起源于 IIS 处理用户和进程的方式.

The source of the issue is fairly hard to debug, because the problem originated in how IIS handles users and processes.

正如我所想,代码本身没有任何问题.

As I thought, there was nothing wrong with the code itself.

回答

在 IIS 中,网站在 AppPool 中运行.AppPool 被分配了一个用户身份.默认身份是一个名为 ApplicationPoolIdentity 的虚拟内置帐户.该用户无权调用任何(据我所知)外部批处理/命令脚本.

In IIS, a website is running in a AppPool. An AppPool is assigned an user identity. The default identity is a virtual built-in account named ApplicationPoolIdentity. This user does not have the privilege to call any (as far as I know) external batch/command scripts.

在开始新进程时为管理用户提供用户名、密码和域,对我来说并没有解决任何问题 - 可能是我误解了整个概念.

Providing a username, password and domain for a administrative user when starting a new process, didn't solve anything for me - It might be that I'm just misunderstanding the whole concept.

在 webconfig 中使用 <identity impersonate="true" userName="domainuser" password="pass"/> 也没有解决任何问题.这显然是因为分配的 AppPool 用户仍然是所有进程的作者.

Using <identity impersonate="true" userName="domainuser" password="pass" /> in the webconfig didn't solve anything either. This is apparently because the assigned AppPool user is still the author of all processes.

真正让我烦恼的是,我可以执行 .exe 文件,但不能执行 .cmd 或 .bat 文件.

What really bugged me out, was that I could execute .exe files, but not .cmd or .bat files.

我的解决方案是创建一个具有执行批处理脚本权限的新用户,并选择该用户作为 IIS 中的 AppPool 用户.

正如我在评论中提到的,与我合作的用户是在 Active Directory 服务器上创建的,因为此特定文件服务器位于网络共享上.用户是我的网络服务器上本地服务器组 IIS_IUSRS 的一部分,并且在存储可执行程序的文件夹中具有读/写/执行权限.

As I have mentioned in the comments, the user I'm working with is created on an Active Directory server as this particular file server is on a network share. The user is part of the local server group IIS_IUSRS on my webserver and has read/write/execute privileges in the folder where the executable programs are stored.

Edit2:该解决方案适用于本地用户帐户,只要用户是本地服务器组 IIS_IUSRS 的一部分并且在存储可执行程序的文件夹.

The solution works for local user accounts as well as long as the user is part of the local server group IIS_IUSRS and has read/write/execute privileges in the folder where the executable programs are stored.

这篇关于Foo.cmd 不会在进程中输出行(在网站上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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