簡體   English   中英

Set-ExecutionPolicy 不受限制的權限被拒絕

[英]Set-ExecutionPolicy unrestricted permission denied

我正在嘗試將執行策略設置為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默認為整個系統設置腳本執行策略(隱含-Scope LocalMachine )。

-Scope LocalMachine只能從提升的 session(以管理員身份運行)使用; [1]如果您的 session 沒有提升,您將看到您看到的錯誤 - 錯誤文本實際上既解釋了問題又提供了如何解決問題的說明。

總結一下:

  • 要么:從提升的 session 重新運行您的命令,假設您具有管理憑據。

    • 您可以使用Start-Process powershell -Verb RunAs啟動一個(在 PowerShell (Core) 7+ 中使用pwsh )。
  • 或者:僅為當前用戶更改持久執行策略( -Scope CurrentUser

     Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force

筆記:

  • 我已選擇RemoteSigned作為示例調用中的策略,因為它在安全性和便利性之間提供了平衡:它對本地腳本沒有限制,但會阻止執行從 web 下載的未經加密簽名的腳本。

  • -Force繞過交互式提示。

  • 雖然當前用戶執行策略優先於本地計算機執行策略,但兩者都可以被基於 GPO 的策略搶占 - 請參閱此答案以獲取更多信息。

  • 還有一種方法可以為單個 session設置執行策略,通過
    -Scope Process ,盡管通常通過 PowerShell CLI 使用( powershell.exe用於 Windows PowerShell, pwsh用於 PowerShell(核心)7+),形式為
    -ExecutionPolicy Bypass


[1] 雖然原則上這同樣適用於 PowerShell (Core) 7+,但如果您恰好將其安裝在當前用戶位置,則不需要提升。 另請注意,當您在類 Unix 平台上使用 PowerShell (Core) 7+ 時,執行策略根本不適用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM