Run Powershell as impersonated user

  • Thread starter Thread starter Tobias BR
  • Start date Start date
T

Tobias BR

Guest
Dear all,

I have a ASP.NET website developed in C#. I´m trying to impersonate a user to run a powershell session as a different user.

Here is my code:

WindowsIdentity.RunImpersonated(CredAcc.GetAdminToken(), () =>
{
WSManConnectionInfo connectionInfo = new WSManConnectionInfo() { };
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();

PowerShell psi = PowerShell.Create();
psi.Runspace = runSpace;
psi.AddScript("echo $env:UserName");

logger.Debug(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
result = psi.Invoke();
});



System.Security.Principal.WindowsIdentity.GetCurrent().Name show´s me that my code runs as the impersonated user. But the powershell session is not impersonated. The echo command shows the normal user. Why???

Thanks for you help.

Best,
Tobias

Continue reading...
 
Back
Top