[英]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政策生效)
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.