从C#运行Powershell时出现错误:`在此系统上禁用了运行脚本`

从C#运行Powershell时出现错误:`在此系统上禁用了运行脚本`,c#,powershell,C#,Powershell,在我的C#应用程序中,我尝试创建一个运行空间,以调用一些Powershell脚本。但是,当它到达实际创建它的代码时,它会失败,错误如下: var implementedHost = new implementedPSHost(); using (var rs = RunspaceFactory.CreateRunspace(customPSHost)) { 无法加载,因为在此系统上禁用了运行脚本。有关更多信息,请参阅上的关于执行策略https://go.microsoft.com/fwlink

在我的C#应用程序中,我尝试创建一个
运行空间
,以调用一些Powershell脚本。但是,当它到达实际创建它的代码时,它会失败,错误如下:

var implementedHost = new implementedPSHost();
using (var rs = RunspaceFactory.CreateRunspace(customPSHost))
{
无法加载
,因为在此系统上禁用了运行脚本。有关更多信息,请参阅上的关于执行策略https://go.microsoft.com/fwlink/?LinkID=135170.At 行:1个字符:3+\Scripts\loadFunctions.ps1+


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

试着在运行空间中运行
Set ExecutionPolicy-Scope CurrentUser
步骤,如下所示

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

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");
而且,没有错误!但是,这将改变用户执行策略,我认为这是不好的行为,因此我将用它包装我的函数,并将用户执行策略设置回我发现的方式

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");
            }
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
Open();
RunspaceInvoke scriptInvoker=新的RunspaceInvoke(运行空间);
scriptInvoker.Invoke(“$ExecutionPolicy=Get ExecutionPolicy-Scope CurrentUser”);
调用(“设置ExecutionPolicy Unrestricted-Scope CurrentUser”);
Pipeline Pipeline=runspace.CreatePipeline();
命令scriptCommand=new命令(“C:\\temp\\test.ps1”);
Collection commandParameters=new Collection();
pipeline.Commands.Add(scriptCommand);
对象集合;
psObjects=pipeline.Invoke();
调用(“Set ExecutionPolicy-Scope CurrentUser$ExecutionPolicy-Force”);
}

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

C应用程序是否在同一用户的安全上下文中运行?@Rohindhart我如何确认?你能分享你正在使用的完整代码吗,错误并不是真正在
using
语句中产生的,但是,当您尝试启动脚本时,我们需要查看整个脚本以了解您在做什么。我从哪里获得
RunspaceInvoke
?Visual Studio抱怨它没有添加对
System.Management.Automation.dll
的引用,这些引用将在
C:\Program Files(x86)中找到\引用Assemblys\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll
是的,我已经使用System.Management.Automation了,因为我已经在使用运行空间了,如果您添加了引用,etcRunspaceInvoke是System.Management.dll的成员,也要确保使用System.Management.Automation将其添加到
.cs
文件顶部的用法中:)
;使用System.Management.Automation.Runspaces是的。我正在使用版本
6.2.1.
。也许这就是问题所在?
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");
            }