Running Powershell from C# gives error: `running scripts is disabled on this system`

自古美人都是妖i 提交于 2021-02-10 12:38:43

问题


In my C# application, I try to create a RunSpace to invoke some Powershell scripts. However when it reaches the code to actually create it, it fails with the error below:

var implementedHost = new implementedPSHost();
using (var rs = RunspaceFactory.CreateRunspace(customPSHost))
{

cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.At line:1 char:3+ . .\Scripts\loadFunctions.ps1+

As suggested elsewhere, I ran a Powershell window and executed Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted but this did not help. The error still happens. Does anyone have any ideas how to fix this?


回答1:


Try running your Set-ExecutionPolicy -Scope CurrentUser step within the runspace, like this.

First, with my ExecutionPolicy for this user to Restricted, and I see the following error when i run this code:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

            using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                runspace.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
                //scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser ");
                Pipeline pipeline = runspace.CreatePipeline();
                Command scriptCommand = new Command("C:\\temp\\test.ps1");
                Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();

                pipeline.Commands.Add(scriptCommand);
                Collection<PSObject> psObjects;
                psObjects = pipeline.Invoke();
            }


 RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
 scriptInvoker.Invoke("Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted");

Gives me

System.Management.Automation.PSSecurityException: 'File C:\temp\test.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.'

Next, I rerun it, uncommenting this line:

scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser ");

And, no errors! However, this will change the users ExecutionPolicy, which I think is bad behavior, so I'm going to instead wrap my function with this and set the Users ExecutionPolicy back the way I found it.

using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                runspace.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
                scriptInvoker.Invoke("$ExecutionPolicy = Get-ExecutionPolicy -Scope CurrentUser");
                scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser ");
                Pipeline pipeline = runspace.CreatePipeline();
                Command scriptCommand = new Command("C:\\temp\\test.ps1");
                Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();

                pipeline.Commands.Add(scriptCommand);
                Collection<PSObject> psObjects;
                psObjects = pipeline.Invoke();
                scriptInvoker.Invoke("Set-ExecutionPolicy -Scope CurrentUser $ExecutionPolicy -Force");
            }

Now our function works and we don't tamper with the user's system settings.



来源:https://stackoverflow.com/questions/56445198/running-powershell-from-c-sharp-gives-error-running-scripts-is-disabled-on-thi

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