C# .ps1 无法加载,因为在此系统上禁用了脚本的执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16460163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:03:21  来源:igfitidea点击:

.ps1 cannot be loaded because the execution of scripts is disabled on this system

c#asp.netpowershell

提问by umesh.chape

I run this code to execute PowerShell code from an ASP.NET application:

我运行此代码以从 ASP.NET 应用程序执行 PowerShell 代码:

System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
runspace.Open();
System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(@"\servername\path");

pipeline.Commands.Add("Out-String");

Collection<PSObject> results = pipeline.Invoke();

runspace.Close();

But I am getting an error:

但我收到一个错误:

.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

.ps1 无法加载,因为在此系统上禁用了脚本的执行。有关更多详细信息,请参阅“get-help about_signing”。

The same code runs fine from a command prompt or a windows (Windows Forms) application.

相同的代码在命令提示符或 Windows(Windows 窗体)应用程序中运行良好。

回答by ghord

Your script is blocked from executing due to the execution policy.

由于执行策略,您的脚本被阻止执行。

You need to run PowerShell as administrator and set it on the client PC to Unrestricted. You can do that by calling Invoke with:

您需要以管理员身份运行 PowerShell,并将其在客户端 PC 上设置为不受限制。你可以通过调用 Invoke 来做到这一点:

Set-ExecutionPolicy Unrestricted

回答by Matthew Steeples

The problem is that the execution policy is set on a per user basis. You'll need to run the following command in your application every time you run it to enable it to work:

问题是执行策略是基于每个用户设置的。每次运行应用程序时,您都需要在应用程序中运行以下命令以使其正常工作:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned

There probably is a way to set this for the ASP.NET user as well, but this way means that you're not opening up your whole system, just your application.

可能还有一种方法可以为 ASP.NET 用户设置它,但这种方法意味着您不会打开整个系统,而只是打开您的应用程序。

(Source)

来源

回答by Nadeem_MK

You need to run Set-ExecutionPolicy:

你需要运行Set-ExecutionPolicy

Set-ExecutionPolicy Unrestricted <-- Will allow unsigned PowerShell scripts to run.

Set-ExecutionPolicy Restricted <-- Will not allow unsigned PowerShell scripts to run.

Set-ExecutionPolicy RemoteSigned <-- Will allow only remotely signed PowerShell scripts to run.

回答by Jon Crowell

There are certain scenarios in which you can follow the steps suggested in the other answers, verify that Execution Policy is set correctly, and still have your scripts fail. If this happens to you, you are probably on a 64-bit machine with both 32-bit and 64-bit versions of PowerShell, and the failure is happening on the version that doesn't have Execution Policy set. The setting does not apply to both versions, so you have to explicitly set it twice.

在某些情况下,您可以按照其他答案中建议的步骤操作,验证是否正确设置了执行策略,但脚本仍然会失败。如果您遇到这种情况,您可能在 64 位计算机上同时使用 32 位和 64 位版本的 PowerShell,并且失败发生在没有设置执行策略的版本上。该设置不适用于两个版本,因此您必须明确设置两次。

Look in your Windows directory for System32 and SysWOW64.

在您的 Windows 目录中查找 System32 和 SysWOW64。

Repeat these steps for each directory:

对每个目录重复这些步骤:

  1. Navigate to WindowsPowerShell\v1.0 and launch powershell.exe
  2. Check the current setting for ExecutionPolicy:

    Get-ExecutionPolicy -List

  3. Set the ExecutionPolicy for the level and scope you want, for example:

    Set-ExecutionPolicy -Scope LocalMachine Unrestricted

  1. 导航到 WindowsPowerShell\v1.0 并启动 powershell.exe
  2. 检查 ExecutionPolicy 的当前设置:

    Get-ExecutionPolicy -List

  3. 为您想要的级别和范围设置 ExecutionPolicy,例如:

    Set-ExecutionPolicy -Scope LocalMachine Unrestricted

Note that you may need to run PowerShell as administrator depending on the scope you are trying to set the policy for.

请注意,您可能需要以管理员身份运行 PowerShell,具体取决于您尝试为其设置策略的范围。

You can read a lot more here: Running Windows PowerShell Scripts

您可以在此处阅读更多内容:运行 Windows PowerShell 脚本

回答by Chandra sekhar P

I had a similar issue and noted that the default cmd on Windows Server 2012 was running the x64 one.

我有一个类似的问题,并注意到 Windows Server 2012 上的默认 cmd 运行的是 x64 的。

For Windows 7, Windows 8, Windows Server 2008 R2 or Windows Server 2012, run the following commands as Administrator:

对于 Windows 7、Windows 8、Windows Server 2008 R2 或 Windows Server 2012,以管理员身份运行以下命令:

x86

x86

Open C:\Windows\SysWOW64\cmd.exe Run the command: powershell Set-ExecutionPolicy RemoteSigned

打开 C:\Windows\SysWOW64\cmd.exe 运行命令:powershell Set-ExecutionPolicy RemoteSigned

x64

x64

Open C:\Windows\system32\cmd.exe Run the command powershell Set-ExecutionPolicy RemoteSigned

打开 C:\Windows\system32\cmd.exe 运行命令 powershell Set-ExecutionPolicy RemoteSigned

You can check mode using

您可以使用检查模式

In CMD: echo %PROCESSOR_ARCHITECTURE% In Powershell: [Environment]::Is64BitProcess

在 CMD 中:echo %PROCESSOR_ARCHITECTURE% 在 Powershell 中:[Environment]::Is64BitProcess

I hope this help you.

我希望这对你有帮助。