PSSecurityException之PowerShell权限设置

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

.\XXXX.ps1 : File
XXXX.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\XXXX.ps1 params[] ...
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

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

Set-ExecutionPolicy RemoteSigned

image

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

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

  • C:\Windows\System32\WindowsPowerShell\v1.0
  • C:\Windows\SystemWoW64\WindowsPowerShell\v1.0
Get-ExecutionPolicy

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

 

参考:https://social.technet.microsoft.com/Forums/en-US/3bd44cf2-684c-4c10-9102-26f2a7b7741d/cant-run-scripts?forum=winserverpowershell

posted @ 2015-03-12 18:01  维博.WILBUR  阅读(14231)  评论(0编辑  收藏  举报