Foo.cmd won't output lines in process (on website)

23,330

Solution 1

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

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.

Answer

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.

Using <identity impersonate="true" userName="domain\user" 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.

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

The solution for me, was to create a new user with privileges to execute batch scripts and select that user as the AppPool user in IIS.

Edit: 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: 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.

Solution 2

you will need to use it this way

   using (Process p = Process.Start(oInfo))
    {
.....

Reason is becuase Process.Start() and Process.Star(startinfo) works slightly different

Process.Start() - Starts (or reuses) the process resource that is specified by the StartInfo property of this Process component and associates it with the component.

Return Value

Type: System.Boolean true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused).

Process.Start(StartInfo) - Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component.

Return Value

Type: System.Diagnostics.Process A new Process component that is associated with the process resource, or null if no process resource is started (for example, if an existing process is reused).

Solution 3

This is slightly modified code but it should give you a better idea on the class

ProcessStartInfo info = new ProcessStartInfo(); 
info.Arguments = "/C C:\Program Files (x86)\itms\foo.cmd"; 
info.WindowStyle = ProcessWindowStyle.Hidden; 
info.CreateNoWindow = true; 
info.FileName = "cmd.exe"; // or C:\Program Files (x86)\itms\foo.cmd with no info.Arguments 
info.UseShellExecute = false; 
info.RedirectStandardOutput = true; 
using (Process process = Process.Start(info)) 
{ 
    using (StreamReader reader = process.StandardOutput) 
    { 
        string result = reader.ReadToEnd(); 
        Console.WriteLine(result);
    } 
} 

This will redirect the output of the cmd window to the Console, just adjust as needed.

Share:
23,330
Marle1
Author by

Marle1

Updated on November 22, 2020

Comments

  • Marle1
    Marle1 over 3 years

    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.

    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)\itms\foo.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/>");
    }
    

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

    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.

    Thanks

    EDIT

    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.

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

    Could this actually be a permission issue?

  • Marle1
    Marle1 almost 12 years
    Thank you for the clarification, HatSoft.
  • Marle1
    Marle1 almost 12 years
    Thank you Bali C, but unfortunately there's still no output returned.
  • Bali C
    Bali C almost 12 years
    Ok, I did run it on my PC and it worked fine, it may be spaces in the file path causing it, try enclosing the path with \"'s like "/C \"C:\Program Files (x86)\itms\foo.cmd\"";
  • Marle1
    Marle1 almost 12 years
    Well, it does work for me locally, but not when I run the code on our website. Could the issue be related to permissions? Is websites somehow limited to executing commands through cmd.exe and denied all output? Sounds rather strange though
  • Bali C
    Bali C almost 12 years
    To be honest I'm not sure, it does sound strange though your right. I don't do much with websites so unfortunately I can't help you there :(
  • HatSoft
    HatSoft almost 12 years
    @Marle1 did my answer helped you and have it resolved your issue
  • Marle1
    Marle1 almost 12 years
    Im afraid not HatSoft. I've edited my question to better reflect my problem, which is maybe linked to user restrictions of some sort.
  • Jack Frost
    Jack Frost almost 9 years
    I know this is a veeeerrrryyy old post but I just want to know what kind of "new user" are you referring to? New Windows user?
  • Marle1
    Marle1 almost 9 years
    Yes or network user. We created a user in our AD and used that as the identity in the AppPool.
  • Jack Frost
    Jack Frost almost 9 years
    I see. I created an admin user and still no go.. I can't even pull up an exe file nor any file..
  • coder3521
    coder3521 over 6 years
    Please post where and how to create the user with such a privilege
  • Sagar
    Sagar over 6 years
    @Marle1, I am also facing a similar issue. After hosting an application on IIS, Application is not able to execute commands on cmd. Did you find any workaround?
  • Marle1
    Marle1 over 6 years
    @Sagar, I have describe the solution that worked for me to the best of my ability, but if it does not work for you, please let me what of the above you have tried, and what issue you are facing.
  • David Christopher Reynolds
    David Christopher Reynolds over 5 years
    Changing the identity the app pool runs under worked for me. My boss said a better option (instead of calling Process.Start) would be to put the functionality you need in a windows service and call that - apparently IIS isn't really designed to hook up to Process.