[英]Set-ExecutionPolicy unrestricted permission denied
I am trying to set the execution policy to Unrestricted
, but I'm getting the following error:我正在尝试将执行策略设置为Unrestricted
,但出现以下错误:
PS> Set-ExecutionPolicy Unrestricted
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
Set-ExecutionPolicy : Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution
policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To
change the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".
At line:1 char:1
+ Set-ExecutionPolicy unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyComma
nd
Set-ExecutionPolicy
defaults to setting the script execution policy for the whole system (implied -Scope LocalMachine
). Set-ExecutionPolicy
默认为整个系统设置脚本执行策略(隐含-Scope LocalMachine
)。
-Scope LocalMachine
can only be used from an elevated session (run as admin); -Scope LocalMachine
只能从提升的 session(以管理员身份运行)使用; [1] if your session isn't elevated, you'll get the error you saw - and the error text actually both explains the problem and provides instructions for how to resolve it. [1]如果您的 session 没有提升,您将看到您看到的错误 - 错误文本实际上既解释了问题又提供了如何解决问题的说明。
To summarize:总结一下:
Either: Re-run your command from an elevated session, assuming you have administrative credentials.要么:从提升的 session 重新运行您的命令,假设您具有管理凭据。
Start-Process powershell -Verb RunAs
(use pwsh
in PowerShell (Core) 7+).您可以使用Start-Process powershell -Verb RunAs
启动一个(在 PowerShell (Core) 7+ 中使用pwsh
)。 Or: Change the persistent execution policy only for the current user ( -Scope CurrentUser
)或者:仅为当前用户更改持久执行策略( -Scope CurrentUser
)
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force
Note:笔记:
I've chosen RemoteSigned
as the policy in the sample call, as it provides a balance between security and convenience: it places no restriction on local scripts, but prevents execution of scripts downloaded from the web that aren't cryptographically signed.我已选择RemoteSigned
作为示例调用中的策略,因为它在安全性和便利性之间提供了平衡:它对本地脚本没有限制,但会阻止执行从 web 下载的未经加密签名的脚本。
-Force
bypasses the interactive prompt. -Force
绕过交互式提示。
While a current-user execution policy takes precedence over a local-machine one, both can be preempted by GPO-based policies - see this answer for more information.虽然当前用户执行策略优先于本地计算机执行策略,但两者都可以被基于 GPO 的策略抢占 - 请参阅此答案以获取更多信息。
There's also a way to set the execution policy for a single session only, via还有一种方法可以为单个 session设置执行策略,通过-Scope Process
, though that is typically used via the PowerShell CLI ( powershell.exe
for Windows PowerShell, pwsh
for PowerShell (Core) 7+), in the form of -Scope Process
,尽管通常通过 PowerShell CLI 使用( powershell.exe
用于 Windows PowerShell, pwsh
用于 PowerShell(核心)7+),形式为-ExecutionPolicy Bypass
. -ExecutionPolicy Bypass
。
[1] While the same applies to PowerShell (Core) 7+ in principle, elevation is not required if you happen to have installed it in a current-user location . [1] 虽然原则上这同样适用于 PowerShell (Core) 7+,但如果您恰好将其安装在当前用户位置,则不需要提升。 Also note that execution policies fundamentally do not apply when you use PowerShell (Core) 7+ on Unix-like platforms .另请注意,当您在类 Unix 平台上使用 PowerShell (Core) 7+ 时,执行策略根本不适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.