2017-04-08 518 views
2

我將它編譯爲可執行文件,但要打開它,我必須右鍵單擊並按「以管理員身份運行」。我希望它在每次運行時都要求管理員權限,但如何執行?如何將管理員權限添加到AutoHotkey腳本?

我不能做到這一點:

因爲當我將它複製到另一臺計算機就不能正常工作。

回答

3

嘗試添加該到自動執行段(腳本的頂部):

; If the script is not elevated, relaunch as administrator and kill current instance: 

full_command_line := DllCall("GetCommandLine", "str") 

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)")) 
{ 
    try ; leads to having the script re-launching itself as administrator 
    { 
     if A_IsCompiled 
      Run *RunAs "%A_ScriptFullPath%" /restart 
     else 
      Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" 
    } 
    ExitApp 
} 

並重新編譯腳本。

欲瞭解更多詳情,請閱讀https://autohotkey.com/docs/commands/Run.htm#RunAs