2

I want to execute some PowerShell script through C# but it requires admin privilege. This is my code (I got it here):

using (new Impersonator("user", "domain", "password"))
{
    // create Powershell runspace
    Runspace runspace = RunspaceFactory.CreateRunspace();

    // open it
    runspace.Open();

    // create a pipeline and feed it the script text
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(scriptText);

    // add parameters if any
    foreach (var parameter in parameters)
    {
        pipeline.Commands[0].Parameters.Add(parameter.Key, parameter.Value);
    }

    // add an extra command to transform the script
    // output objects into nicely formatted strings

    // remove this line to get the actual objects
    // that the script returns. For example, the script

    // "Get-Process" returns a collection
    // of System.Diagnostics.Process instances.
    pipeline.Commands.Add("Out-String");

    // execute the script
    Collection<PSObject> results = pipeline.Invoke();

    // close the runspace
    runspace.Close();

    // convert the script result into a single string
    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

Anyway, this doesn't work on my machine. For example, if the script text is "Set-ExecutionPolicy Unrestricted" then I get "Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied."

And in my case, it cannot get list of virtual machines through Get-VM command. (I found that Get-VM only return results if it runs under Admin privilege.)

Do I do something wrong? Is there another solution for this problem?

7
  • Can't you just run it as administrator? Commented Oct 22, 2013 at 11:58
  • I run it through C#, so I am trying to figure it out how to do that. :) Commented Oct 22, 2013 at 12:00
  • If you run the application as admin (or if running it directly from visual studio, run visual studio as admin), the process it starts should automatically be ran as admin. Commented Oct 22, 2013 at 12:04
  • This code will be part of a website and running a website as admin is not allowed. Commented Oct 22, 2013 at 12:22
  • Is it a good idea to access the registry and/or execute power-shell through a website? Commented Oct 22, 2013 at 12:29

1 Answer 1

5

This will launch PowerShell as an Administrator:

var newProcessInfo = new System.Diagnostics.ProcessStartInfo();
newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
newProcessInfo.Verb = "runas";
System.Diagnostics.Process.Start(newProcessInfo);

If you need to pass in a script to run, then use:

newProcessInfo.Arguments = @"C:\path\to\script.ps1";
Sign up to request clarification or add additional context in comments.

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.