簡體   English   中英

從autohotkey運行Powershell腳本時執行策略錯誤

[英]Execution Policy error when running powershell script from autohotkey

我有一個簡單的自動熱鍵腳本

!+l:: RunWait, PowerShell -NoExit -C "Import-Module D:\projects\changescreensaver\changescreensaver.psm1; Set-ScreenSaverTimeout 1;"

但這不會讓我加載我的ps配置文件或執行導入模塊,並給我執行策略錯誤:

Import-Module : File D:\projects\changescreensaver\changescreensaver.psm1 cannot be loaded because running scripts is
disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.

在我的終端上, Get-ExecutionPolicy -List返回

C:\Users\someone>Get-ExecutionPolicy  -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine    RemoteSigned 

但在我的腳本中,返回

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined

我可以將它傳遞給-ExecutionPolicy Bypass ,但是我仍然想理解:從AHK調用PS時,為什么我的ExecutionPolicy值不同?

您找到了問題的根源 -您的AHK是32位的,因此運行32位的PowerShell可執行文件,而您的終端(控制台窗口)運行了64位的可執行文件-但讓我添加一些背景信息。

Windows PowerShell中 (不同於PowerShell Core中 ), 執行策略存儲在注冊表中 ,即:

  • LocalMachine范圍:

    • HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\PowerShell\\1\\ShellIds\\Microsoft.PowerShell ,值ExecutionPolicy
  • CurrentUser范圍:

    • HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\PowerShell\\1\\ShellIds\\Microsoft.PowerShell ,值ExecutionPolicy

雖然32位和64位應用程序共享相同的HKEY_CURRENT_USER配置單元,但它們具有不同的HKEY_LOCAL_MACHINE\\Software子樹

因此, 只有LocalMachine范圍才具有32位和64位可執行文件的單獨執行策略值。

換句話說:如果執行策略是在用戶級別( -Scope CurrentUser )設置的,則不會出現此問題。

原來我的終端運行的是64位Powershell,而AHK使用的是32位,通過運行以下命令發現:

[Environment]::Is64BitProcess

根據這個帖子

暫無
暫無

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

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