Powershell / windows终端 无法加载文件 因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170

问题描述在 powershell或者 windows 终端中运行脚本文件时,提示“无法加载文件 因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。”PS C:\Users\ciels\Desktop> .\test.ps1.\test.ps1 : 无法加载文件 C:\Users\ciels\Desktop\test.ps1,因为在此系统上禁.
摘要由CSDN通过智能技术生成

问题描述

在 powershell 或者 windows 终端中运行脚本文件时,提示“无法加载文件 因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.m
icrosoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。”

PS C:\Users\ciels\Desktop> .\test.ps1
.\test.ps1 : 无法加载文件 C:\Users\ciels\Desktop\test.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.m
icrosoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\test.ps1
+ ~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

问题原因

PowerShell 或者 windows 终端启动时,为了确保安全,文件的执行策略默认设置为 Restricted 。 Restricted 执行策略不允许任何脚本运行。  

有关文件的执行策略可参考:

关于执行策略 - PowerShell | Microsoft Docs介绍 PowerShell 执行策略并说明如何管理它们。icon-default.png?t=N7T8https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

其中写到:Restricted 执行策略允许单独的命令,但不允许任何脚本运行,将阻止运行所有脚本文件,包括格式设置和配置文件 (.ps1xml) 、模块脚本文件 (.psm1) 和 PowerShell 配置文件 (.ps1) 。

而 AllSigned 和 RemoteSigned 执行策略可防止 Windows PowerShell 运行没有数字签名的脚本。

AllSigned 要求所有脚本和配置文件都由受信任的发布者签名,包括在本地计算机上编写的脚本。

Bypass 没有任何内容被阻止,也没有警告或提示。

Default 默认执行策略。Windows客户端受限或Windows服务器远程登录。

RemoteSigned 要求从Internet下载的所有脚本和配置文件都由受信任的发布者签名。Windows server计算机的默认执行策略。

Restricted 不加载配置文件或运行脚本。Windows客户端计算机的默认执行策略。

Undefined 没有为作用域设置执行策略。从未由组策略设置的范围中删除分配的执行策略。如果所有作用域中的执行策略都未定义,则有效的执行策略将受到限制。

Unrestricted 从PowerShell 6.0开始,这是非Windows计算机的默认执行策略,无法更改。加载所有配置文件并运行所有脚本。如果您运行的是从internet下载的未签名脚本,则在运行之前系统会提示您输入权限。

解决方法

因此只需要降低安全性等级,将执行策略改为 RemoteSigned 即可执行脚本,在窗口中使用 Set-ExecutionPolicy 命令即可。

关于 Set-ExecutionPolicy 命令可参考:

Set-ExecutionPolicy (Microsoft.PowerShell.Security) - PowerShell | Microsoft DocsThe Set-ExecutionPolicy cmdlet changes PowerShell execution policies for Windows computers. For more information, see about_Execution_Policies. Beginning in PowerShell 6.0 for non-Windows computers, the default execution policy is Unrestricted and can't be changed. The Set-ExecutionPolicy cmdlet is available, but PowerShell displays a console message that it's not supported. An execution policy is part of the PowerShell security strategy. Execution policies determine whether you can load configuration files, such as your PowerShell profile, or run scripts. And, whether scripts must be digitally signed before they are run. The Set-ExecutionPolicy cmdlet's default scope is LocalMachine, which affects everyone who uses the computer. To change the execution policy for LocalMachine, start PowerShell with Run as Administrator. To display the execution policies for each scope in the order of precedence, use Get-ExecutionPolicy -List. To see the effective execution policy for your PowerShell session use Get-ExecutionPolicy with no parameters.icon-default.png?t=N7T8https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.2

具体方法

以管理员身份打开 PowerShell 或者 windows 终端,执行命令 set-executionpolicy remotesigned 并确认。

set-executionpolicy remotesigned

 然后再次运行该脚本文件即可通过。

恢复默认设置

通过修改将降低系统的安全性,可能带来被病毒或恶意软件攻击的风险。

因此,在执行完自己编写的脚本文件后,可以将设置改回默认,执行以下命令即可 set-executionpolicy Restricted

set-executionpolicy Restricted

同样需要进行确认。

其他

如果需要了解计算机上的现用执行策略,可以打开PowerShell 然后输入 get-executionpolicy

get-executionpolicy

 更多关于执行策略的设置,可以参阅Set-ExecutionPolicy (Microsoft.PowerShell.Security) - PowerShell | Microsoft Docs进行配置。

参考链接:

https://www.jianshu.com/p/4eaad2163567
https://blog.csdn.net/Gabriel_wei/article/details/102449719

  • 13
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值