4

I have the following code that I have tested and works:

    using (new Impersonator("Administrator", "dev.dev", #########"))
    {
        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);

        runspace.Open();

        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");

        Pipeline pipeline = runspace.CreatePipeline();
        Command myCmd = new Command(@"C:\test.ps1");
        myCmd.Parameters.Add(new CommandParameter("upn", upn));
        myCmd.Parameters.Add(new CommandParameter("sipAddress", sipAddress));
        pipeline.Commands.Add(myCmd);

        // Execute PowerShell script
        Collection<PSObject> results = pipeline.Invoke();
    }

However, when I try to include the function in a different project so that it is called from a webservice it throws an execption:

    System.Management.Automation.CmdletInvocationException: Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. ---> System.UnauthorizedAccessException: Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

I have no idea why this is happening. Any help would be appreciated.

2
  • What were you planning on doing with the impersonator object? Since at the moment you do not seem to be using it... Commented Jul 4, 2011 at 7:02
  • It was my attempt to fix the problem. It was posted as a solution for a similar problem. Commented Jul 4, 2011 at 7:14

2 Answers 2

8

What's happening is that Impersonator only impersonates on the thread, and PowerShell's Runspace is running on another thread.

To make this work, you need to add:

runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = System.Management.Automation.Runspaces.PSThreadOptions.UseCurrentThread;

just before you open the runspace.

This will force the runspace to run on the same thread as the impersonated token.

Hope this helps,

Sign up to request clarification or add additional context in comments.

3 Comments

Indeed I did. I fixed the answer.
thanks for the answer. I couldn't found any namespace for the ThreadOptions.
That's because I made a typo. The class is System.Management.Automation.PSThreadOptions
1

Use these namespaces :

using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;

Create Runspace with InitialSessionState

InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
initialSessionState.ApartmentState = ApartmentState.STA;
initialSessionState.ThreadOptions = PSThreadOptions.UseCurrentThread;

using ( Runspace runspace = RunspaceFactory.CreateRunspace ( initialSessionState ) )
{
  runspace.Open();

  // scripts invocation                 

  runspace.Close();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.