2

I'm trying to run a batch file using Process.Start

I'm using the following code :

Process myProcess = new Process();
myProcess.StartInfo.FileName = "D:\\test.bat";
myProcess.StartInfo.UserName = "user";
var strPass = new SecureString();

for (int i = 0; i < strUserPass.Length; i++)
    {
        strPass.AppendChar(strUserPass[i]);
    }
strPass.MakeReadOnly();

myProcess.StartInfo.Password = strPass;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;

myProcess.Start();

I'm using ASP.NET MVC 3 and IIS 7.5

When I run this code on my local computer (Windows 7), it works perfectly.

When I run this code on my server (Windows Server 2008 R2), it does not work. The process start but the batch file is not executed and the process doesn't have any StandardOutput or StandardError. I cannot get any information and there is no error.

If I don't specify any UserName and Password, it works on my server. The batch file is executed with my Network Service account.

I really need to execute the process with a specific Username.

Do you know why it doesn't work?

---EDIT : Microsoft Support found an explanation

Since Windows Vista et sessions isolations, services and user applications are executed in different sessions.

Windows does not allow to execute a process from a session to another session with other credentials. The process will be executed with session's default credentials (AppPool credentials).

More info here and here

Toc
  • 163
  • 2
  • 9

1 Answers1

0

Have you tried with myProcess.StartInfo.CreateNoWindow = true; ?

I had found this great piece of code once (I guess here on SO) which works fine for me:

    private bool RunProcess(string executableProgram, string arguments, int timeOut)
    {
        bool Result = false;
        int exitCode = 0;

        using (Process process = new Process())
        {
            process.StartInfo.FileName = executableProgram;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            StringBuilder output = new StringBuilder();
            StringBuilder error = new StringBuilder();

            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
            using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
            {
                process.OutputDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        outputWaitHandle.Set();
                    }
                    else
                    {
                        output.AppendLine(e.Data);
                    }
                };
                process.ErrorDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        errorWaitHandle.Set();
                    }
                    else
                    {
                        error.AppendLine(e.Data);
                    }
                };

                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                if (process.WaitForExit(timeOut) && outputWaitHandle.WaitOne(timeOut) && errorWaitHandle.WaitOne(timeOut))
                {
                    exitCode = process.ExitCode;
                    Result = (exitCode == 0);
                }
                else
                {
                    // Timed out.
                    Result = false;
                }
            }

            if (!string.IsNullOrEmpty(output.ToString()))
            {
                Logger.Info(output.ToString());
            }
            if (!string.IsNullOrEmpty(error.ToString()))
            {
                Logger.Error(error.ToString());
            }

        }
        return (Result);
    }
LeftyX
  • 35,328
  • 21
  • 132
  • 193
  • Yes. It doesn't change anything. The problem appears when I use credentials (StartInfo.UserName & StartInfo.Pass). If I don't use any, default IIS credentials are used and it works. – Toc May 14 '12 at 09:28
  • @Toc: since you're using IIS7, your app is using the AppPool Credentials. – LeftyX May 14 '12 at 09:33
  • Yes but I want to force other Credentials to execute this process. – Toc May 14 '12 at 15:05