title: How to run executable from PHP website as a specific Windows user?
body: <p>By default PHP runs under <strong>IUSR</strong> account. When executed directly:</p>
  
  <pre><code>$lastline = exec('D:\\MyProgram.exe', $output, $return_var);
  </code></pre>
  
  <p>It runs, but the program is unable to do it's tasks due to insufficient privileges. My question is: <strong>How do I run an executable under a specific Windows account from a PHP website?</strong></p>
  
  <h2>I've tried:</h2>
  
  <p>When executed through <a href="http://technet.microsoft.com/en-us/sysinternals/bb897553" rel="nofollow">Sysinternals PsExec</a>:</p>
  
  <pre><code>$lastline = exec('D:\\PsExec.exe -u UserName -p Password -accepteula "D:\\MyProgram.exe"', $output, $return_var);
  </code></pre>
  
  <p>MyProgram.exe does not even execute at all. PHP throws empty output and return_var is <code>-1073741502</code>. I assume this is some sort of unhandled exception.</p>
  
  <hr>
  
  <p>When executed through <a href="http://www.moernaut.com/default.aspx?item=lsrunas" rel="nofollow">lsrunas</a>:</p>
  
  <pre><code>$lastline = exec('D:\\lsrunas.exe /user:UserName /password:Password /domain:DOMAIN /command:"D:\\MyProgram.exe" /runpath:D:\\', $output, $return_var);
  </code></pre>
  
  <p>MyProgram.exe does not execute either. PHP throws empty output and return_var is <code>0</code>.</p>
  
  <hr>
  
  <p>Microsoft's built in <code>runas</code> command doesn't work because it doesn't accept password as a parameter.</p>
  
  <hr>
  
  <p>Tried using different PHP functions like <code>shell_exec</code>, <code>system</code> and <code>passthru</code></p>
  
  <hr>
  
  <p>Tried creating a new Application Pool under IIS, to run the website under LOCAL SERVICE, SYSTEM, or a specific user.
  EDIT: This is where I went wrong. It should work, but fails when Load User Profile setting is not enabled (step 3 in my answer).</p>
  
  <p><strong>Does anyone have working suggestions?</strong></p>
  
  <p><em>Note: All commands are working and tested through command line directly.</em></p>