How to add administrator privileges to AutoHotkey script?

北战南征 提交于 2019-11-29 07:46:19

Try adding this to the auto-execute section (top of the script):

; 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
}

and recompile the script.

For more details read https://autohotkey.com/docs/commands/Run.htm#RunAs.

Here's a much simpler code for this purpose:

if not A_IsAdmin
Run *RunAs "%A_ScriptFullPath%"

It will run the script as Admin if it's not already running as Admin.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!