Executes the given code as a new AutoHotkey process (As Administrator)

Get help with using AutoHotkey and its commands and hotkeys
m33mt33n
Posts: 4
Joined: 18 Sep 2016, 10:04
Facebook: m33mt33n

Executes the given code as a new AutoHotkey process (As Administrator)

26 Nov 2018, 09:33

Hi,

Can someone help modifying this code from AHK Help file to execute script as admin.

Code: Select all

ExecScript(Script, Wait:=true)
{
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    exec.StdIn.Write(script)
    exec.StdIn.Close()
    if Wait
        return exec.StdOut.ReadAll()
}
or something similar for same purpose.
Thanks
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Executes the given code as a new AutoHotkey process (As Administrator)

26 Nov 2018, 12:57

You don't need to modify it, you just need to start the parent script as admin, and any script launched by ExecScript() will have admin privileges,add snippet below to the autoexec/top/ section of your script from which ExecScript() is executed.

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
}
live ? long & prosper : regards
m33mt33n
Posts: 4
Joined: 18 Sep 2016, 10:04
Facebook: m33mt33n

Re: Executes the given code as a new AutoHotkey process (As Administrator)

26 Nov 2018, 15:23

Thanks for your reply!

But my question is about running a piece of code (by using something like ExecScript function) from a script which is not currently running as admin, for example I have a code which make symlinks and needed to be run as admin for the purpose, and this code is in another script which don't needed to runas admin (or I don't want to run it as admin), currently I doing this by appending code to a temporary file and then running it as admin. but this required extra step of using temp files, it have done some searching for same topic and found that other languages like VB can do this using same COM Objects, also attempted to modify above script for the job, but my knowledge about COM Objects is very limited, and modifications not worked for me.
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Executes the given code as a new AutoHotkey process (As Administrator)

26 Nov 2018, 22:19

TBH I don't really think that's possible, because if it were it would be a huge security hole,because it would allow user mode applications to interact with COM.Objects of privileged processes.

https://docs.microsoft.com/en-us/window ... ellexecute
https://ss64.com/vb/exec.html

VB scripts use https://ss64.com/vb/shellexecute.html to elevate,but Shell.Execute() in that link doesn't return an object,meaning it's more like a RunAs command,which simply won't do as piping to a file'less' script as in ExecScript() interacts with the created process instead of simply running it.

I.e:

Code: Select all

/*
https://docs.microsoft.com/en-us/windows/desktop/shell/shell-shellexecute
Shell.ShellExecute method
Performs a specified operation on a specified file.

https://ss64.com/vb/shellexecute.html
ShellExecute method

Run a script or application in the Windows Shell.

Syntax
      .ShellExecute "application", "parameters", "dir", "verb", window

      .ShellExecute 'some program.exe', '"some parameters with spaces"', , "runas", 1

Key
   application   The file to execute (required)
   parameters    Arguments for the executable
   dir           Working directory
   verb          The operation to execute (runas/open/edit/print)
   window        View mode application window (normal=1, hide=0, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default)

The runas verb is undocumented but can be used to elevate permissions.
*/

ExecScript("MsgBox")

; ExecScript: Executes the given code as a new AutoHotkey process.
ExecScript(Script, Wait:=true)
{
    shell := ComObjCreate("Shell.Application")
    exec := shell.ShellExecute("C:\Program Files\AutoHotkey\AutoHotkey.exe", "/ErrorStdOut *",,"runas")
   ;you can't interact with created process so,no point from here on...
    exec.StdIn.Write(script)
    exec.StdIn.Close()
    if Wait
        return exec.StdOut.ReadAll()
}
live ? long & prosper : regards

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], HiSoKa, TAC109 and 33 guests