【问题标题】:Running Powershell from C# gives error: `running scripts is disabled on this system`从 C# 运行 Powershell 会出现错误:`running scripts is disabled on this system`
【发布时间】:2020-05-14 22:59:36
【问题描述】:

在我的 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,但这并没有帮助。错误仍然发生。有没有人知道如何解决这个问题?

【问题讨论】:

  • C# 应用程序是否在同一用户的安全上下文中运行?
  • @RohinSidharth 我该如何确认?
  • 你能分享你正在使用的完整代码吗,using 语句中并没有真正生成错误,而是当你尝试启动脚本时,我们需要查看全文看看你在做什么。

标签: c# powershell


【解决方案1】:

尝试在运行空间中运行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,我认为这是不好的行为,所以我将用它来包装我的函数,并将用户 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");
            }

现在我们的功能可以正常工作了,我们不会篡改用户的系​​统设置。

【讨论】:

  • 我从哪里获得RunspaceInvoke? Visual Studio 抱怨它不存在
  • 添加对System.Management.Automation.dll 的引用,可在此处找到C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll
  • 是的,我已经拥有using System.Management.Automation,因为我已经在使用Runspace
  • RunspaceInvoke 是 System.Management.dll 的成员,如果您添加了引用,请确保也将其添加到 .cs 文件顶部的 usings 中:) @987654332 @docs.microsoft.com/en-us/dotnet/api/…
  • 我做到了。我正在使用版本6.2.1.。也许这就是问题所在?
猜你喜欢
  • 2020-07-01
  • 1970-01-01
  • 2021-11-21
  • 2021-08-21
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
相关资源
最近更新 更多