以管理员身份运行 AHK 脚本,无需点击“以管理员身份运行”

问题描述 投票:0回答:3

是否可以让 AHK 脚本立即以管理员身份运行,而无需右键单击它?我希望双击脚本时弹出UAC提示。

(抱歉,如果我不好解释我是初学者)

admin autohotkey uac
3个回答
1
投票

您可以使用文档中的这段代码来自动以管理员身份重新启动任何脚本(如果它尚未以管理员身份运行)。

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

if (!(A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)")))
{
    try
    {
        if (A_IsCompiled)
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}

只需确保它位于自动执行部分(脚本的顶部)。


0
投票

创建 AHK 脚本(或从中生成的已编译 EXE)的快捷方式。在快捷方式的属性中,选择“快捷方式”选项卡,然后单击“高级”按钮。在弹出窗口中,选中“以管理员身份运行”框,然后单击“确定”、“应用”、“确定”


0
投票
; Elevate script to run as administrator
If !A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%"  ; Run the script as administrator
    ExitApp  ; Exit the current instance running without admin privileges
}

; Rest of your script goes here
MsgBox, Running with admin privileges!

; Your script code continues...
© www.soinside.com 2019 - 2024. All rights reserved.