如何修复“此系统上禁用运行脚本”?

Tha*_*han 57 powershell

当我尝试在 VS Code 终端上运行 ionic 命令时ionic serve,出现以下错误。

我怎样才能解决这个问题?

ionic : File C:\Users\Lakshan\AppData\Roaming\npm\ionic.ps1 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:1
+ ~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Run Code Online (Sandbox Code Playgroud)

Tha*_*han 154

我找到了修复此错误的方法。

如下:

  1. 首先,使用“以管理员身份运行”打开 PowerShell
  2. 然后,在 PowerShell 中运行此命令
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    
    Run Code Online (Sandbox Code Playgroud)
  3. 之后Y输入并按 Enter 键。

  • “无限制”的执行政策通常被认为是有风险的。更好的选择是“远程签名”,它不会阻止本地创建和存储的脚本,但会阻止从互联网下载的脚本运行,除非您专门检查并取消阻止它们。 (21认同)

pro*_*365 11

这是因为执行策略。这定义了 powershell 脚本的运行方式。

在默认 Windows 桌面中,它是受限制的,不允许任何脚本(签名或未签名)仅交互式会话。

因此,最好的设置是使用RemoteSigned(Windows Server 上的默认值)仅允许远程签名的脚本和本地未签名的脚本运行,但Unrestriced让所有脚本运行是不安全的。

设置Set-ExecutionPolicy -ExecutionPolicy RemoteSigned以管理员身份运行。


小智 8

这段代码将修复它:

\n
Set-ExecutionPolicy RemoteSigned \xe2\x80\x93Scope Process\n
Run Code Online (Sandbox Code Playgroud)\n

  • 这是最安全的选项,因为它只影响当前会话。 (2认同)

Rel*_*som 5

要绕过此问题,您需要更改执行策略。将其输入您的终端。

Set-ExecutionPolicy -Scope CurrentUser
Run Code Online (Sandbox Code Playgroud)

然后它会提示您提供一个值,此时您可以设置绕过/远程签名或限制。

cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: "RemoteSigned" or "Bypass" or "Restricted".
Run Code Online (Sandbox Code Playgroud)