Enabling execution policy for PowerShell from C#

Deadly 提交于 2021-02-08 08:24:42

问题


I have an ASP.NET MVC 4 page that calls a piece of PowerShell. However, I am running into a problem as a module I am using is not signed, so I have to enable the Unrestricted policy. How can I force the PowerShell child to use Unrestricted policy?

I have enabled this in my script, but it is ignored. Also when I try to set the policy in code, an exception is thrown.

    using (Runspace myRunSpace = RunspaceFactory.CreateRunspace())
    {
        myRunSpace.Open();

        using (PowerShell powerShell = PowerShell.Create())
        {
            powerShell.Runspace = myRunSpace;
            powerShell.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted");
            powerShell.AddScript(script);

            objectRetVal = powerShell.Invoke();
        }
    }

回答1:


If you only need to run the one script with no interactions you can set the execution policy via the command prompt like so:

string command = "/c powershell -executionpolicy unrestricted C:\script1.ps1";
System.Diagnostics.Process.Start("cmd.exe",command);



回答2:


This is the same as @kravits88 answer but without displaying the cmd:

static void runPowerShellScript(string path, string args) {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = @"/c powershell -executionpolicy unrestricted " + path + " " + args;
        startInfo.UseShellExecute = false;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.CreateNoWindow = true;
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();
    }



回答3:


You have to use parameter -Scope = CurrentUser:

  powershell.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted")
    .AddParameter("Scope","CurrentUser");



回答4:


My solution was to self sign the modules and script I was running from IIS Express. I'm still developing and have found that IIS Express does not see all modules that you may have installed in the \System32\WindowsPowerShell...\Modules Path. I moved the modules I was using to another drive and used that location to import the module into my script.

Thanks for the replies :-)




回答5:


For PowerShell 5.1 and PowerShell 7 Core, you can use an ExecutionPolicy Enum to set the Execution policy, like so:

using Microsoft.PowerShell;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
...
public class MyClass
{
     public void MyMethod() 
     {
          // Create a default initial session state and set the execution policy.
          InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
          initialSessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted;

          // Create a runspace and open it. This example uses C#8 simplified using statements
          using Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionState);
          runspace.Open();

          // Create a PowerShell object 
          using PowerShell powerShell = PowerShell.Create(runspace);

          // Add commands, parameters, etc., etc.
          powerShell.AddCommand(<command>).AddParameter(<parameter>);

          // Invoke the PowerShell object.
          powerShell.Invoke()
     }
}


来源:https://stackoverflow.com/questions/13420799/enabling-execution-policy-for-powershell-from-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!