Run Powershell as impersonated user RRS feed

  • Question

  • 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();
                    PowerShell psi = PowerShell.Create();                
                    psi.Runspace = runSpace;
                    psi.AddScript("echo $env:UserName");
                    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.

    Wednesday, August 29, 2018 11:15 AM

All replies