Ensuring Process.Start() runs under the logged in user
<p>I'm running a batch file from some ASP.NET/C# code on a web server. Basically the batch file performs some test automation tasks on a VM using tools like psloggedon and pexec.</p>

<p>If I run the batch file manually when I'm logged into the server under an administrative account, it works fine.</p>

<p>My problem comes when I run it from my code (below), it seems to run under the 'SYSTEM' account, and psloggedon etc. don't seem to work correctly.</p>

<p><strong>Code</strong></p>

<pre><code>     Process p = new Process();
     p.StartInfo.FileName = "C:\SetupVM.bat";
     p.Start();
     p.WaitForExit();
</code></pre>

<p>I've got this in my web.config, it doesn't seem to make any differance?</p>

<pre><code>&lt;identity impersonate="true" userName="Administrator" password="myadminpassword"/&gt;
</code></pre>

<p>Is there anyway I can ensure the batch file runs under the 'Administrator' account?</p>

<p><strong>UPDATED CODE</strong></p>

<pre><code>      Process p = new Process();
      p.StartInfo.FileName = "C:\\SetupVM.bat";
      p.StartInfo.UserName = "Administrator";
      p.StartInfo.UseShellExecute = false;
      p.StartInfo.WorkingDirectory = "C:\\";

      string prePassword = "myadminpassword";
      SecureString passwordSecure = new SecureString();
      char[] passwordChars = prePassword.ToCharArray();
      foreach (char c in passwordChars)
      {
          passwordSecure.AppendChar(c);
      }
      p.StartInfo.Password = passwordSecure;
      p.Start();
      p.WaitForExit();
</code></pre>

<p>From MSDN:</p>

<p><em>When UseShellExecute is false, you can start only executables with the Process component.</em></p>

<p>Maybe this is the issue as I'm trying to run a .bat file?</p>

<p>Thanks.</p>
