从C#运行Powershell会产生错误:此系统上禁用了运行脚本。

时间:2019-06-04 13:42:55

标签: c# powershell

在C#应用程序中,我尝试创建一个RunSpace来调用某些Powershell脚本。但是,当到达实际创建的代码时,它将失败,并显示以下错误:

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+

正如其他地方所建议的,我运行了一个Powershell窗口并执行了Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted,但这没有帮助。该错误仍然会发生。有谁知道如何解决此问题?

1 个答案:

答案 0 :(得分:0)

尝试像这样在运行空间中运行Set-ExecutionPolicy -Scope CurrentUser步骤。

首先,将我的用户的ExecutionPolicy设置为Restricted,然后在运行此代码时看到以下错误:

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

给我

  

System.Management.Automation.PSSecurityException:'无法加载文件C:\ temp \ test.ps1,因为在此系统上禁用了运行脚本。有关更多信息,请参阅https://go.microsoft.com/fwlink/?LinkID=135170上的about_Execution_Policies。'

接下来,我重新运行它,取消注释此行:

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

而且,没有错误!但是,此会更改用户的ExecutionPolicy ,我认为这是不良行为,因此我将使用该函数包装函数,并将Users ExecutionPolicy设置为找到它的方式。

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

现在我们的功能起作用了,我们不会篡改用户的系​​统设置。