6

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();
        }
    }
3
  • 2
    What does the exception say? Commented Nov 16, 2012 at 16:45
  • 1
    Does generating a self signed certificate, setting the web app's identity to trust that certificate and then signing the script help? (There does not appear to be a way to create a Runspace with some equivalent of PowerShell.exe's -ExecutionPolicy parameter. Commented Nov 16, 2012 at 17:13
  • Thanks for the pointer. I set my Execution Policy in the Registry for Users and Computers to RemoteSigned. Then I self signed the modules I was using and also the script I am running. Commented Nov 19, 2012 at 16:18

5 Answers 5

18

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()
     }
}
Sign up to request clarification or add additional context in comments.

2 Comments

An important caveat for PS 6 and up if you're not running on Windows, from the documentation: Beginning in PowerShell 6.0 for non-Windows computers, the default execution policy is Unrestricted and can't be changed. The Set-ExecutionPolicy cmdlet is available, but PowerShell displays a console message that it's not supported. Source: learn.microsoft.com/en-us/powershell/module/…
This should be the accepted answer.
12

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

1 Comment

Nice thinking - that's got me to where I need to be now with the other discovery I made.
5

You have to use parameter -Scope = CurrentUser:

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

Comments

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

Comments

0

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

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.