RunAsTask() - Auto-elevates script without UAC prompt

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 204
Joined: 29 Sep 2013, 16:58

RunAsTask() - Auto-elevates script without UAC prompt

21 Aug 2014, 18:01

Code: [Select all] [Expand] [Download] (RunAsTask.ahk)GeSHi © Codebox Plus

Usage example:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

#NoTrayIcon
RunAsTask() ; self elevate
Run RegEdit.exe


RunAsTask() elevates the calling script by registering a scheduled task with highest privileges on the *first run of the script.
For subsequent runs, it will start a parallel elevated instance via Task scheduler and original instance will exit.

*UAC will prompt on the first run.

RunAsTask() generates a unique Taskname , for a given script as in following pattern:
"[RunAsTask]" . A_Space . A_ScriptName . A_Space . "@" . ( CRC32 hash of file paths, as decimal )
eg. [RunAsTask] My Admin Script.ahk @0123456789
For every change in A_ScriptFullpath ( compiled / uncompiled ) or A_AhkPath + A_ScriptFullpath ( uncompiled ), a new task will be registered. It is up to the user to clean and maintain the task scheduler.

Here is a task scheduler export of XML which was registered by RunAsTask(). The items in red are not in script. Items in blue are variable.

Task scheduler XML export


Edit1: 2014/Aug/22 13:25 UTC

RunAsTask_CreateShortcut()
RunAsTask_CreateShortcut() is a supplementary function for RunAsTask(). It will create a shortcut for the Taskname returned by RunAsTask()
Shortcut target example: C:\Windows\system32\schtasks.exe /run /tn "[RunAsTask] MyAdminScript.ahk @3264355656"

Code: [Select all] [Download] (RunAsTask_CreateShortcut.ahk)GeSHi © Codebox Plus

RunAsTask_CreateShortcut( TaskName := "", Folder := "", ShcName := "" ) { ; by SKAN, http://goo.gl/yG6A1F
Local LINK, Description

IfEqual, TaskName,, Return
LINK := ( FileExist( Folder ) ? Folder : A_ScriptDir ) "\" ( ShcName ? ShcName : A_ScriptName ) ".lnk"
FileGetShortcut, %LINK%,,,, Description
If ( Description <> Taskname )
FileCreateShortcut, schtasks.exe, %LINK%, %A_WorkingDir%,/run /tn "%TaskName%", %TaskName%,,,, 7
}
Usage example:

Code: [Select all] [Download] (Test_CreateShortcut.ahk)GeSHi © Codebox Plus

TaskName := RunAsTask()
RunAsTask_CreateShortcut( TaskName ) ; Places a shortcut in Script folder
Parameter examples:

Code: [Select all] [Download] GeSHi © Codebox Plus

RunAsTask_CreateShortcut( TaskName, A_Desktop ) ; Shortcut on desktop
RunAsTask_CreateShortcut( Taskname, A_Startup, "My Admin Script" ) ; Place in Startup. Force link name to avoid multiple startup entries.
User avatar
joedf
Posts: 5416
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: RunAsTask() - Auto-elevates script without UAC prompt

21 Aug 2014, 18:41

Ahhh you're using the "Schtasks with elevate privileges" trick thingy, I remember I used it once in a c# program I wrote, I was thinking of using for aspdm, but then I realized that "dynamic" arguments can not be given without creating the task anew (due to security concerns)... For me I think that's the only disadvantage, it can be worked around, but it would use a small "dirty" trick..

Anyway, Kudos for making a nice simple to use function! Another one for the book, SKAN! ;)
User avatar
Nextron
Posts: 963
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: RunAsTask() - Auto-elevates script without UAC prompt

21 Aug 2014, 18:50

Setting up my main script to run as admin using task scheduler is something I usually only do once per computer. Now I don't have to do it at all :thumbup:, although I added static autorun:=RunAsTask() to the function to make it auto-initialize. After that, you can use this similar script that does the opposite, to run process as unelevated user.
Last edited by Nextron on 21 Aug 2014, 18:56, edited 2 times in total.
The more I know:
The more I know,
I know nothing.
User avatar
joedf
Posts: 5416
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: RunAsTask() - Auto-elevates script without UAC prompt

21 Aug 2014, 18:51

Haha, nice!
User avatar
SKAN
Posts: 204
Joined: 29 Sep 2013, 16:58

RunAsTask_CreateShortcut()

22 Aug 2014, 09:32

Added function: RunAsTask_CreateShortcut()
User avatar
SKAN
Posts: 204
Joined: 29 Sep 2013, 16:58

Re: RunAsTask() - Auto-elevates script without UAC prompt

22 Aug 2014, 09:33

Nextron wrote:run process as unelevated user.
Why not use Run_() or ShellRun() available in Installer.ahk ?

Everything search engine launches everything elevated ( except explorer ).
I have set ShellRun.ahk in Options > General > Context Menu > Open (Files) > Command as $exec("shellrun.ahk" "%1")

Code: [Select all] [Download] (ShellRun.ahk)GeSHi © Codebox Plus

#NoTrayIcon
Param = %1%
ShellRun( Param )
Return

#Include C:\Program Files\AutoHotkey\Installer.ahk ; My A_Ahkpath is different
User avatar
Nextron
Posts: 963
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: RunAsTask_CreateShortcut()

22 Aug 2014, 13:12

SKAN wrote:Added function: RunAsTask_CreateShortcut()
That's freaky. I thought about a feature like this yesterday, but then I thought: "Yeah, like I ever kill the AutoHotkey.exe process :lol:"

SKAN wrote:Why not use Run_() or ShellRun() available in Installer.ahk ?
I've got three function in my main script, RunAsLimitedUser() (launch with restricted security token), shellrun() (launch from user shell object) and RunAsUser() (launch as scheduled task) (all from here). I mostly use RunAsLimitedUser() because it returns me a PID. I've encountered a couple situations where it doesn't work properly and I use shellrun(). I mentioned RunAsUser() for its similarity.
The more I know:
The more I know,
I know nothing.
arcticir
Posts: 405
Joined: 17 Nov 2013, 11:32

Re: RunAsTask() - Auto-elevates script without UAC prompt

22 Aug 2014, 17:13

Get commandline

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

Process, Exist 
CmdLine:=winmgmts("CommandLine","ProcessId",ErrorLevel)

winmgmts(v,w:="ProcessId",n:="CommandLine",d:="Win32_Process",m:="winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"){
s:=[]
for i in ComObjGet(m).ExecQuery("Select * from " d (w ? " Where " w "='" n "'" :""))
s.Insert(i[v])
return s.MaxIndex()?(s.MaxIndex()=1?s.1:s):""
}
User avatar
joedf
Posts: 5416
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: RunAsTask() - Auto-elevates script without UAC prompt

22 Aug 2014, 17:54

@arcticir nice share, but I don't think that's related, or is it?
arcticir
Posts: 405
Joined: 17 Nov 2013, 11:32

Re: RunAsTask() - Auto-elevates script without UAC prompt

22 Aug 2014, 18:51

CmdLine More accurate
User avatar
SKAN
Posts: 204
Joined: 29 Sep 2013, 16:58

Re: RunAsTask() - Auto-elevates script without UAC prompt

23 Aug 2014, 10:52

@arcticir: May I ask how winmgmts() is better than DllCall( "GetCommandLine","Str" ) ? :roll:
you wrote:CmdLine More accurate
I intentionally have it the way it is. If the script is Run from an another script with shortpath, then CRC32 hash will differ and RunAsTask() will register it as a new task. Also there is the Reload command ( also as tray menu item) which inserts a /restart in command line.

joedf wrote:but it would use a small "dirty" trick..
Can you explain?, please. :)
User avatar
joedf
Posts: 5416
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: RunAsTask() - Auto-elevates script without UAC prompt

23 Aug 2014, 12:13

Well, maybe there are other ways to do this, but the only thing that comes to mind right now, is to write a temporary file at a predefined location.. Say, a file like myprogram.arguments.txt in AppData and the task could launch "thing.exe /taskmode" or something close telling it to go read that file... It's not ultra dirty but a little bit ;)
Hey! Maybe this could be a feature... Haha :D

Regards
User avatar
SKAN
Posts: 204
Joined: 29 Sep 2013, 16:58

LaunchAsAdmin

23 Aug 2014, 17:16

joedf wrote:task could launch "thing.exe /taskmode" or something close telling it to go read that file

I did not think of that. Nice, thanks :)

Here is an another way where I rely on OnExit. The template also supports Reload, but is not being used.
As for parsing CmdLine, I will post a function in a *separate topic.

Edit: *Args() - Returns command line parameters as array

Code: [Select all] [Expand] [Download] (LaunchAsAdmin.ahk)GeSHi © Codebox Plus

User avatar
joedf
Posts: 5416
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: RunAsTask() - Auto-elevates script without UAC prompt

23 Aug 2014, 18:37

Oh! Nice, very interesting, I'll have to try it when I return from my trip, thanks for sharing. :)

Edit: cool, Reading from argv pointer!
User avatar
evilC
Posts: 3150
Joined: 27 Feb 2014, 12:30

Re: RunAsTask() - Auto-elevates script without UAC prompt

29 Sep 2015, 05:13

I have successfully used this lib for scripts that run on various people's home PCs, but I now have a need to run this on a VM at work, and I cannot get it to function - it pops up the UAC dialog every time.
Sure, we could turn UAC off, but the use case is automated tests, and turning off UAC is not desirable on these test VMs. Any ideas what may be stopping it from working?
I have tried with just the regedit test script and no joy :(

FYI, I am trying to use it to allow remove machines within the VM environment to read the event log of the Exchange server.
User avatar
evilC
Posts: 3150
Joined: 27 Feb 2014, 12:30

Re: RunAsTask() - Auto-elevates script without UAC prompt

02 Nov 2015, 11:24

I replaced run_as_admin in my project with RunAsTask, and now I am getting many, many reports of the application no longer starting.
I have managed to replicate the issue on a VM - every EXE I compile with RunAsTask suffers from the same issues if run on a PC with UAC on:

On double-clicking of the compiled EXE, nothing happens. The script does not run, no UAC prompt appears.
The only way to get the script to run is to right-click the EXE and select "Run as Administrator". The UAC prompt then appears, and the application runs.

Example script:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Gweek
Posts: 64
Joined: 24 Sep 2015, 06:29

Re: RunAsTask() - Auto-elevates script without UAC prompt

16 Dec 2015, 21:07

joedf wrote:Oh! Nice, very interesting, I'll have to try it when I return from my trip, thanks for sharing. :)

Edit: cool, Reading from argv pointer!


You are about too read some bad english. and i am really sorry about it i am not a native english speaker

Hey joedf how are U ? hope you are fine :)
i just started using Ahk. and i too don't like UAC prompts .until now i was using task scheduler to skip UAC prompts but now i think i could handle this problem with the help of ahk

so can you plz tell me this script that SKAN created .its only able to skip uac only when i write application name on this script what i want is universal action i want to run this script and boom every application skips uac.
so can you plz tell me how should i modify this script for my use & plz understand i am just foolish noob so plz bear with me

some important facts
i am running windows 10 and don't want to disable uac for some specific reasons
when i run this script every program i click or opens indirectly will skip UAC.
i do a lot of networking work safely without uac on windows 7 but rightnow i have to done some of my works on windows 10 so i want to make this script to easy my burden(UAC are time consuming )

thanxx plz relpy i came here with a lot of hope so plz dont break my hope
User avatar
joedf
Posts: 5416
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: RunAsTask() - Auto-elevates script without UAC prompt

17 Dec 2015, 16:38

Well that would be a big security breach... You could have a "central" executable run all your other scripts since the rights will be inherited.
Gweek
Posts: 64
Joined: 24 Sep 2015, 06:29

Re: RunAsTask() - Auto-elevates script without UAC prompt

17 Dec 2015, 18:13

joedf wrote:Well that would be a big security breach... You could have a "central" executable run all your other scripts since the rights will be inherited.


I already know the consequences and so i already bought 24/7 backup station in the case my windows fuckup so plz Now i just want your help that's all i want

Thanxx

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Randy31416 and 16 guests