简体   繁体   English

由于执行策略,Visual Studio 无法执行 Powershell 脚本

[英]Visual Studio can't execute a Powershell script due to execution policy

I'm trying to run a basic powershell script in a c# application, but I can't seem to get it to run.我正在尝试在 c# 应用程序中运行基本的 powershell 脚本,但我似乎无法运行它。 I've tried this code with and without the added execution policy code in the pipeline creation and have gotten the same results.我已经尝试过这段代码,无论是否在管道创建中添加了执行策略代码,都得到了相同的结果。

static void Main(string[] args)
    {
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        Pipeline pipeline = runspace.CreatePipeline("Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned");


        pipeline.Commands.Add(@"C:\Users\Bob\Desktop\testScript.ps1");

        // Execute PowerShell script
        var results = pipeline.Invoke();
    }

The error I receive is this:我收到的错误是这样的:

System.Management.Automation.PSSecurityException: 'File C:\Users\Bob\Desktop\testScript.ps1 cannot be loaded because running scripts is disabled on this system. System.Management.Automation.PSSecurityException: '文件 C:\Users\Bob\Desktop\testScript.ps1 无法加载,因为在此系统上禁用运行脚本。 For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.'有关详细信息,请参阅https://go.microsoft.com/fwlink/?LinkID=135170 上的 about_Execution_Policies。

When I run "get-executionpolicy -list", my execution policy seems to be fine.当我运行“get-executionpolicy -list”时,我的执行策略似乎没问题。

在此处输入图像描述

  • Normally, the persistently configured script execution policy applies to PowerShell SDK -based projects such as yours, too.通常,持久配置的脚本执行策略也适用于基于PowerShell SDK的项目,例如您的项目。

  • While your Get-ExecutionPolicy -List output implies that RemoteSigned is the effective policy (as reported when you omit -List ), the color of your screenshots suggest that you were asking for Windows PowerShell 's policy, which is separate from the execution policy for the cross-platform, install-on demand PowerShell (Core) v6+ edition.虽然您的Get-ExecutionPolicy -List output 暗示RemoteSigned是有效的策略(如您省略-List时所报告的那样),但屏幕截图的颜色表明您正在要求Windows PowerShell的策略,它与执行策略分开跨平台,按需安装PowerShell (Core) v6+版。

  • Therefore, your inability to execute a local script - which RemoteSigned should allow - can have one of two causes:因此,您无法执行本地脚本( RemoteSigned应该允许)可能有以下两个原因之一:

    • If you project is based on the SDK for PowerShell (Core) v6+ (via the Microsoft.PowerShell.SDK NuGet package), you'd have to consult - and possibly modify - its effective execution policy (run pwsh -c Get-ExecutionPolicy to see the policy in effect)如果您的项目基于 SDK for PowerShell (Core) v6+ (通过Microsoft.PowerShell.SDK NuGet 包),您必须咨询 - 并可能修改 -它的有效执行策略(运行pwsh -c Get-ExecutionPolicy以查看 the政策生效)

      • If you don't actually have PowerShell (Core) 6+ itself installed (ie if you're only using its SDK ), use the solution in the bottom section - which may be preferable anyway.如果您实际上没有安装 PowerShell (Core) 6+ 本身(即,如果您只使用它的SDK ),请使用底部的解决方案 - 无论如何这可能更可取。
    • As Mathias points out in a comment, another possible reason is that your local script may have been downloaded from the web , via a web browser, in which case it is considered a remote file for the purpose of the execution policy.正如 Mathias 在评论中指出的那样,另一个可能的原因是您的本地脚本可能是通过 web 浏览器从 web下载的,在这种情况下,出于执行策略的目的,它被视为远程文件。 Passing its file path to Unblock-File as a one-time action should fix the problem.将其文件路径作为一次性操作传递给Unblock-File应该可以解决问题。


Taking a step back:退一步说:

If you trust the local script to invoke, you can bypass the execution policy for your application only , by configuring your PowerShell SDK session accordingly - see this answer .如果您信任要调用的本地脚本,则可以通过相应地配置您的 PowerShell SDK session 来绕过您的应用程序的执行策略- 请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "如何使用 c# 执行 powershell 脚本并设置执行策略?" - How to execute a powershell script using c# and setting execution policy? 在 Visual Studio C# 中执行 Powershell 脚本后结果为空 - Null results after execution Powershell script in Visual Studio C# Powershell:执行政策 - Powershell : Execution Policy 我无法在Visual Studio 2012上执行项目 - I can't execute a project on Visual Studio 2012 由于服务器执行失败,Visual Studio模拟器无法启动 - Visual Studio emulator failed to Start due to Server execution failure 由于 XmlSerialization (sgen.exe) 并且无法禁用,无法在 Visual Studio 2022 中构建项目 - Can't Build Project In Visual Studio 2022 Due To XmlSerialization (sgen.exe) And Can't Disable 由于奇怪的错误,无法在Visual Studio 2008 C#Windows窗体中看到设计器视图 - Can't see the designer view in Visual Studio 2008 C# Windows Forms due to weird error 似乎无法从asp.net C#简单执行PowerShell脚本 - Can't seem to do a simple execute of PowerShell script from asp.net C# 在 C# Web API 中执行 Powershell 脚本时执行策略错误 - Execution Policy Error while executing Powershell script in C# Web API 从WPF应用程序调用Powershell脚本不执行脚本 - Calling powershell script from WPF application doesn't execute script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM