RunAsTask() - Auto-elevates script without UAC prompt

Post your working scripts, libraries and tools for AHK v1.1 and older
kriszty
Posts: 4
Joined: 02 Dec 2020, 05:11

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

02 Dec 2020, 05:15

Stupid question maybe...

The runastask works great... but in my code my next line has to start but without admin rights is there a simple solution for this
because if i punt for example the line run app3.exe it aso starts with admin rights, but now it has to run in normal users mode

runastask.stop or exit ?
jsong55
Posts: 225
Joined: 30 Mar 2021, 22:02

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

10 May 2021, 20:13

The above solution doesn't work for me.

In addition, I noticed something peculiar.

When I run an ahk file from VSCode there's no UAC prompt

Whereas if I were to run directly by double clicking in file explorer or using one AHK script to run another with Run, scriptname.ahk then the UAC prompt appears
Mika_erdo
Posts: 41
Joined: 30 Jul 2020, 17:23

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

30 May 2021, 13:55

Hey I use your script and it works fine for most of the time but I also get this message box

Ewj1aC5H8W.jpg
Ewj1aC5H8W.jpg (7.91 KiB) Viewed 3562 times

if I already have a script running as elevated, running it again from explorer window(with RunAsTask) causes the above message box to show.
Its frustrating as I do this all the time, for example to refresh a running script.

Does anyone have any workarounds? Thanks.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

30 May 2021, 14:18

Mika_erdo wrote:
30 May 2021, 13:55
if I already have a script running as elevated, running it again from explorer window(with RunAsTask) causes the above message box to show.
That will be the the behaviour even without RunAsTask().

Edit:
The following workaround should work:
https://www.autohotkey.com/boards/viewtopic.php?p=72017#p72017
Mika_erdo
Posts: 41
Joined: 30 Jul 2020, 17:23

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

30 May 2021, 15:02

Thats excellent SKAN. it did just the trick. I did not know that it was the case even without your script, I stand corrected.

I love your tools and they proven to be a boon to me and my unknowing sister. Much thanks!
Nite
Posts: 1
Joined: 27 Sep 2021, 17:24

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

29 Sep 2021, 05:40

Hi,
I'm new to AHK.
My problem is "very simple" :
I have a .ahk script which I need to run on startup (Windows 10).
I placed the script file into the startup dir of Windows. Doing so, the script is launched after login, but the problem is I am prompted every single time by UAC to confirm running it.
Searching around I was pointed to this thread. The problem is I really didn't get how to make use of RunAsTask.
Could you walk me through it "step by step" ?
At the moment I simply have the .ahk file.

Thanks
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

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

21 Nov 2021, 18:01

Does this script make any changes to the Windows Registry?
Last edited by carno on 26 Jul 2022, 12:50, edited 1 time in total.
Stealthle
Posts: 32
Joined: 13 Mar 2017, 00:48

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

18 Jul 2022, 22:51

Thank you Skan! Any idea why this would work fine on an uncompiled script but on the same script compiled it closes the original script as it is supposed to but doesnt start a new,elevated one?
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

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

26 Jul 2022, 12:52

Nite wrote:
29 Sep 2021, 05:40
Hi,
I'm new to AHK.
My problem is "very simple" :
I have a .ahk script which I need to run on startup (Windows 10).
I placed the script file into the startup dir of Windows. Doing so, the script is launched after login, but the problem is I am prompted every single time by UAC to confirm running it.
Searching around I was pointed to this thread. The problem is I really didn't get how to make use of RunAsTask.
Could you walk me through it "step by step" ?
At the moment I simply have the .ahk file.

Thanks
I am also interested in this "step by step" walk-through as my coding is getting a bit rusty.
darknessdanielc
Posts: 4
Joined: 25 Jan 2018, 05:35
Contact:

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

12 Feb 2023, 11:57

Worked like a charm!

Thanks so much, I stumbled across this and tried with my Disable AltTab script (I play a lot of Apex, so need to run this to avoid accidentally pressing Windows key / Alt Tab stuff. Doesn't kick in unless it's running as an Admin because Apex does).

I can see in Task Manager it's elevated (can choose "Elevated" from the Details visible columns). As a thank you, I don't mind sharing my Disable AltTab script for you all :) You can use the F8 key to Suspend or Resume it too!

I had to redirect all separate hotkeys to Return, otherwise it would still exit the game if another was pressed!
Attachments
Disable AltTab.ahk
(407 Bytes) Downloaded 92 times
stiuna
Posts: 13
Joined: 13 Jun 2020, 23:11

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

22 Jul 2023, 08:24

SKAN wrote:
21 Aug 2014, 18:01
Your script has an error, in these lines:

Code: Select all

CmdLine       := ( A_IsCompiled ? "" : """"  A_AhkPath """" )  A_Space  ( """" A_ScriptFullpath """"  )
TaskName      := "[RunAsTask] " A_ScriptName " @" SubStr( "000000000"  DllCall( "NTDLL\RtlComputeCrc32"
                   , "Int",0, "WStr",CmdLine, "UInt",StrLen( CmdLine ) * 2, "UInt" ), -9 )
The correct way is as follows:

Code: Select all

CmdLine       := ( A_IsCompiled ? "" : """"  A_AhkPath """" )  A_Space  ( """" A_ScriptFullpath """"  )
CmdLine       := Trim(CmdLine, A_Space)
TaskName      := "[RunAsTask] " A_ScriptName " @" SubStr( "000000000"  DllCall( "NTDLL\RtlComputeCrc32"
                   , "Int",0, "WStr",CmdLine, "UInt",StrLen( CmdLine ) * 2, "UInt" ), -9 )
Without Trim function when the script is compiled the UAC prompt does not appear which is necessary the first time, but not only that but the program closes because Run uses CmdLine and tries to run the .exe with a space added at the beginning and that path does not exist so the program crashes.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

22 Jul 2023, 10:26

stiuna wrote:
22 Jul 2023, 08:24
Your script has an error, in these lines:

Code: Select all

CmdLine       := ( A_IsCompiled ? "" : """"  A_AhkPath """" )  A_Space  ( """" A_ScriptFullpath """"  )
 
 
Good catch. :( . Thanks! :thumbup:
Modified it to:
 

Code: Select all

 CmdLine       := ( A_IsCompiled ? "" : """"  A_AhkPath " """ ) . ( """" A_ScriptFullpath """"  )
 
 
Edit: V2 version is here RunAsTask() (25-Jul-2023)
 
stiuna
Posts: 13
Joined: 13 Jun 2020, 23:11

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

22 Jul 2023, 21:37

@SKAN That's better, I thought spaces are necessary to separate arguments, I did a quick test and your line works fine.
Philister
Posts: 33
Joined: 01 Apr 2016, 07:31

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

18 Sep 2023, 01:45

Greetings!

Here is how I would like to use this script. Is it possible and how would I have to modify it? Too daft to figure it out myself just now.

Run the first instance elevated through Windows task scheduler every time the PC starts, but have it do nothing except wait to be called with a simple numeric command line argument.

When it is run as e. g.

Code: Select all

RunAsTask.ahk 1
, it should run a certain command with admin privileges (I know how to check command line args and call the stuff I want). In my case, I want it to shutdown my PC. Currently, I can't get Autohotkey's Shutdown command to work unless the command is issued with admin privileges.

I tried making the script persist to wait for further calls with command line arguments but then on the second call with the argument, I got an error message that the first instance could not be closed, would I want to continue waiting?

Is this even a good idea or is there a simpler way to reliably shutdown a PC from AHK when the main script isn't run with admin privilegs without having to click through a UAC prompt every time?

Many thanks!
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

18 Sep 2023, 08:32

Philister wrote:
18 Sep 2023, 01:45
Currently, I can't get Autohotkey's Shutdown command to work unless the command is issued with admin privileges.
I don't understand! why?
This version (for AHK 1.1) of RunAsTask() doesn't process command line parameters.
 

Code: Select all

#NoTrayIcon
RunAsTask() ; self elevate 
Shutdown 5
Philister
Posts: 33
Joined: 01 Apr 2016, 07:31

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

18 Sep 2023, 09:44

Thanks for your reply, SKAN!
SKAN wrote:
18 Sep 2023, 08:32
Philister wrote:
18 Sep 2023, 01:45
Currently, I can't get Autohotkey's Shutdown command to work unless the command is issued with admin privileges.
I don't understand! why?
Me neither but if I create a script Shutdown.ahk with the only command Shutdown, 1 and execute it from Total Commander (running non-privileged), nothing happens. When I right-click on the script and run it as administrator, I get a UAC prompt and the PC shuts down on confirmation. Hence, I'm looking for a way just to run any kind of shutdown command with admin privileges without having to go through the UAC prompt.
This version (for AHK 1.1) of RunAsTask() doesn't process command line parameters.
I know, I want to add that part to the script that contains RunAsTask(). So run it once as admin from a task scheduler startup event without parameters to evade UAC, then eventually call it from another script with 1 as command line parameter to have it shutdown the PC rather than go through the RunAsTask() function.

Code: Select all

#NoTrayIcon
RunAsTask() ; self elevate 
Shutdown 5
But that will bring up the UAC dialog, won't it? That's what I'm trying to avoid.

Maybe I'm having a major brain meltdown and am missing the purpose of RunAsTask(). I thought that once I've run it as admin from a task scheduler startup event, I could use it to execute some commands (shutdown, mostly) with admin privilege by calling the script with a certain command line parameter. That's how I understood the introduction to the OP:
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.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

18 Sep 2023, 11:28

Philister wrote:
18 Sep 2023, 09:44
if I create a script Shutdown.ahk with the only command Shutdown, 1 and execute it from Total Commander (running non-privileged), nothing happens. When I right-click on the script and run it as administrator, I get a UAC prompt and the PC shuts down on confirmation.
 
What happens when you click launch Shutdown.ahk without admin privileges :?
 
Philister wrote:
18 Sep 2023, 09:44

Code: Select all

#NoTrayIcon
RunAsTask() ; self elevate 
Shutdown 5
But that will bring up the UAC dialog, won't it? That's what I'm trying to avoid.

Maybe I'm having a major brain meltdown and am missing the purpose of RunAsTask().
 
UAC dialog will occur only on the first run.
Philister
Posts: 33
Joined: 01 Apr 2016, 07:31

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

18 Sep 2023, 13:54

SKAN wrote:
18 Sep 2023, 11:28
What happens when you click launch Shutdown.ahk without admin privileges :?
Literally nothing. The script runs but the shutdown command has no effect. I've tried other command line-based methods of shutting down the PC, e. g. nircmd, but none have any effect on my PC unless run with admin privilege.
UAC dialog will occur only on the first run.
I know, but since I run shutdown once every session, I'd rather it'd work without a UAC prompt. Also, I thought if I first ran the script containing RunAsTask() with admin privilege via a task scheduler event on startup, I wouldn't need the prompt on subsequent calls?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

18 Sep 2023, 21:17

Philister wrote:
18 Sep 2023, 13:54
SKAN wrote:
18 Sep 2023, 11:28
What happens when you click launch Shutdown.ahk without admin privileges :?
Literally nothing. The script runs but the shutdown command has no effect.
 
Try to fix that problem instead of working it around.
Philister
Posts: 33
Joined: 01 Apr 2016, 07:31

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

19 Sep 2023, 09:31

Isn't it the default to have to run shutdown.exe (plus, I assume, other shutdown commands like a shutdown.ahk) from an elevated command prompt? This article suggests elevating by default. Thanks!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: mprost and 129 guests