Add Run as Admin

Propose new features and changes
illuzioner
Posts: 26
Joined: 07 Jun 2017, 13:50

Add Run as Admin

20 Jun 2017, 09:29

Often times I need to run a script as admin. I have to do this manually since none of the options I've come across are viable.

Of course security is a big concern, so a password or some sort of security to prevent hacking is necessary, but it would help in a number of important cases to have that automatic ability.
User avatar
WAZAAAAA
Posts: 75
Joined: 13 Jan 2015, 19:48

Re: Add Run as Admin

20 Jun 2017, 11:29

The Run documentation has this: https://autohotkey.com/docs/commands/Run.htm#RunAs
Try adding the following code at the top of your script:

Code: Select all

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

if not (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
}

MsgBox A_IsAdmin: %A_IsAdmin%`nCommand line: %full_command_line%
Tested on XP/W7/W8.1/W10, works with both compiled and non-compiled scripts.
YOU'RE NOT ALEXANDER

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 33 guests