简体   繁体   English

从 C# 启用 PowerShell 的执行策略

[英]Enabling execution policy for PowerShell from C#

I have an ASP.NET MVC 4 page that calls a piece of PowerShell.我有一个调用 PowerShell 的 ASP.NET MVC 4 页面。 However, I am running into a problem as a module I am using is not signed, so I have to enable the Unrestricted policy.但是,我遇到了一个问题,因为我使用的模块没有签名,所以我必须启用Unrestricted策略。 How can I force the PowerShell child to use Unrestricted policy?如何强制 PowerShell 孩子使用不受限制的策略?

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();
        }
    }

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);

You have to use parameter -Scope = CurrentUser:您必须使用参数-Scope = CurrentUser:

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

This is the same as @kravits88 answer but without displaying the cmd:这与@kravits88 的答案相同,但不显示 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();
    }

For PowerShell 5.1 and PowerShell 7 Core, you can use an ExecutionPolicy Enum to set the Execution policy, like so:对于 PowerShell 5.1 和 PowerShell 7 Core,您可以使用ExecutionPolicy Enum来设置执行策略,如下所示:

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()
     }
}

My solution was to self sign the modules and script I was running from IIS Express.我的解决方案是对我从 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.我仍在开发中,发现 IIS Express 没有看到您可能已安装在 \\System32\\WindowsPowerShell...\\Modules 路径中的所有模块。 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 :-)感谢您的回复:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM