简体   繁体   English

Set-ExecutionPolicy 不受限制的权限被拒绝

[英]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 重新运行您的命令,假设您具有管理凭据。

    • You can start one with 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.

相关问题 在“ Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force”之后还原回PowerShell - Revert Back PowerShell after “Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force” 如何使用多个命令和Set-ExecutionPolicy Unrestricted -force制作.ps1文件? - How to make a .ps1 file with multiple commands and Set-ExecutionPolicy Unrestricted -force? Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 的安全风险 - Security risks of Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 使用PowerShell脚本为数百个用户提供Set-ExecutionPolicy - Set-ExecutionPolicy for hundreds of users with PowerShell script Windows 7 上的 PowerShell:为普通用户设置执行策略 - PowerShell on Windows 7: Set-ExecutionPolicy for regular users 如何为 Set-Executionpolicy 创建 Powershell 别名 - How to create a Powershell Alias for Set-Executionpolicy 为特定脚本全局设置 ExecutionPolicy - Set-ExecutionPolicy gloablly for a specific script Set-ExecutionPolicy哪个是正确的? - Set-ExecutionPolicy which is the right one? 运行 `Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass` 是否安全 - Is it safe to run `Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass` 运行任何Powershell脚本时出现Set-ExecutionPolicy错误 - Getting Set-ExecutionPolicy error while running any powershell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM