PSSecurityException之PowerShell权限设置

Windows下PowerShell默认的权限级别是Restricted,不允许执行PS脚本(即.ps1文件)。如果在Restricted权限级别下运行,会得到错误信息:

vue : 无法加载文件 C:\Users\29506\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Executi 
on_Policies。
所在位置 行:1 字符: 1
+ vue init nuxt/starter
+ ~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException

要解决这类问题,通常的做法是,用管理员权限启动PS命令行,将执行权限修改为RemoteSigned或者Unrestricted:

Set-ExecutionPolicy RemoteSigned

到这里,一般就可以解决大多数情况下的问题。

但是,有的时候会发现有的PS脚本还是会抛出上面的错误。如果你的系统是64位的Windows,那么有可能你执行脚本是调用的powershell.exe并不是你改过权限的那一个。从下面两个位置下运行powershell并查看权限设置:

C:\Windows\System32\WindowsPowerShell\v1.0

C:\Windows\SystemWoW64\WindowsPowerShell\v1.0

Get-ExecutionPolicy

如果有Restricted将其改为RemoteSigned或者Unrestricted。

更多Windows powershell 执行策略的调整,请参考https://blog.csdn.net/Jeffxu_lib/article/details/84710386

猜你喜欢

转载自www.cnblogs.com/yihengbaobei/p/12935334.html