700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Powershell / windows终端 无法加载文件 因为在此系统上禁止运行脚本。有关详细信

Powershell / windows终端 无法加载文件 因为在此系统上禁止运行脚本。有关详细信

时间:2018-09-13 18:27:18

相关推荐

Powershell / windows终端 无法加载文件 因为在此系统上禁止运行脚本。有关详细信

问题描述

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

/fwlink/?LinkID=135170 中的 about_Execution_Policies。”

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

问题原因

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

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

关于执行策略 - PowerShell | Microsoft Docs介绍 PowerShell 执行策略并说明如何管理它们。/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./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进行配置。

参考链接:

/p/4eaad2163567

/Gabriel_wei/article/details/102449719

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。