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