none
Run Powershell as impersonated user

    Pertanyaan

  • 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
    Rabu, 29 Agustus 2018 11.15

Semua Balasan