问题
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