我有一个调用 PowerShell 的 ASP.NET MVC 4 页面.但是,我遇到了一个问题,因为我使用的模块没有签名,所以我必须启用 Unrestricted 策略.如何强制 PowerShell 子级使用 Unrestricted 策略?
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(); } } 推荐答案如果您只需要运行一个脚本而无需交互,您可以通过命令提示符设置执行策略,如下所示:
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);更多推荐
从 C# 启用 PowerShell 的执行策略
发布评论