title: Remote PowerShell sessions from C# under ApplicationPoolIdentity
body: <p>Using a <a href="http://archive.msdn.microsoft.com/psoutlooklive" rel="nofollow noreferrer">Library</a> I found from Microsoft, I have been attempting (with C#) to provision email accounts for my users with Live@Edu and the library uses remote PowerShell sessions to do this. I have wrapped the PowerShell calls with a <code>using() { }</code> block that impersonates a local administrator account. When I run the code on my own development machine it works great and provisions the account on Live@Edu, yet when I run the same code on the production server I get an <code>Access is Denied</code> error from PowerShell. </p>
  
  <p>What I just noticed is if I change the IIS Application Pool user on the server to my own domain account everything works fine on the production server, but leaving it as <code>ApplicationPoolIdentity</code> does not work. So it appears that even though in my code I impersonate a local administrator, those credentials are not being passed on to the PowerShell session. Oddly enough, when the scripts run on my own machine, also under <code>ApplicationPoolIdentity</code>, there is no problem and that leads me to believe that the script is actually running under my own account on my machine (and I am a local administrator).</p>
  
  <p>I did have the code spit out the value of <code>$env:username</code> and it gave me the machine name and I was expecting it to give me the actual username it runs under since that is what I get when I type that command directly into a PowerShell window.</p>
  
  <p>When logged in to the remote server interactively using the credentials that I have been impersonating in code, I can manually type all of the PowerShell cmdlts into a PowerShell window and they work fine.</p>
  
  <p>I don't want to have my IIS Application Pool always running under an administrator account as that sees foolish so is there a way of running the PowerShell script as an administrator that goes further than the current impersonation that I am doing?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>There was an odd thing that happened that sees to work as a solution for me. After deploying my code to the server I created added a local administrator account. I then went to the IIS Application Pool and changed the owner from <code>ApplicationPoolIdentity</code> to the admin account I just created. After that the page would work fine to run the PowerShell script. I already knew this from before, but didn't want to have IIS using an admin account. I then proceeded to set the Application Pool back to <code>ApplicationPoolIdentity</code> and removed the Local Admin account and the page still works!? I restarted IIS, and the Web Server itself, and everything works. All I can think is that moving the Application Pool over to an Admin account changed some attribute in the App pool permanently. I have now put a <a href="https://serverfault.com/questions/423001/does-changing-an-iis-application-pool-owner-permanently-modify-attributes">modified question on ServerFault</a>.</p>