RunAsTask() - Auto-elevates script without UAC prompt

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
joedf
Posts: 8937
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

21 Jan 2016, 10:12

this script only simplifies the work to do, in order to create a "UAC-skipping" shortcut.
If you want everything without UAC, then you might as well disable it.
Sorry :(
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Gweek
Posts: 64
Joined: 24 Sep 2015, 06:29

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

21 Jan 2016, 10:18

joedf wrote:this script only simplifies the work to do, in order to create a "UAC-skipping" shortcut.
If you want everything without UAC, then you might as well disable it.
Sorry :(
actually that's the problem for some metro apps i am working on and developing on.if i disable the uac they will disable too. :(
User avatar
joedf
Posts: 8937
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

21 Jan 2016, 10:44

Oh metroapps? nooooo nooo.... that's not going to work... They launch by app-references not by file path...
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Gweek
Posts: 64
Joined: 24 Sep 2015, 06:29

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

21 Jan 2016, 12:49

joedf wrote:Oh metroapps? nooooo nooo.... that's not going to work... They launch by app-references not by file path...
you didn't understand i am not bypassing uac for metroapps i am bypassing uac for some other networking work.........but sadly uac just a nuisance right now ...it makes my work tooo hard
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

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

21 Oct 2016, 07:06

I have been looking into why RunAsTask was not working for me in some instances with compiled scripts.

The problem seems to be with this line:
Run *RunAs %CmdLine%, %A_ScriptDir%, UseErrorLevel
If CmdLine contains "D:\Some Path\Some Script.exe" (The string contains quotes) then AHK tries to exectue Run, *RunAs "D:\Some Path\Some Script.exe" which fails with a "File not found" error. Instead the command-line should be Run, *RunAs D:\Some Path\Some Script.exe

Here is my temporary fix:

Code: Select all

  If ( not A_IsAdmin and not TaskExists )  {
	CLine       := ( A_IsCompiled ? A_ScriptFullpath  : """"  A_AhkPath """ """ A_ScriptFullpath """" )
    Run *RunAs %CLine%, %A_ScriptDir%, UseErrorLevel
	OutputDebug % "AHK| RunAsAdmin ErrorLevel is: " ErrorLevel ", A_LastError is: " A_LastError
	if (ErrorLevel != "ERROR")
		ExitApp
   }
Also, what's the deal with the statement "It is up to the user to clean and maintain the task scheduler."? I looked in the task scheduler and there were a couple of entries which appeared to be old and no longer needed - is this list potentially going to bloat and bloat over time?
Is it technically feasible to programatically maintain this list? ie for a given filename, ensure there is only ever one entry in the scheduler?

It looks like you should be able to call GetTasks which will give you an IRegisteredTaskCollection.
Any ideas on how to enumerate that?
I guess you could search for any tasks that match the pattern [RunAsTask] <ScriptName> but have a different number after the @ (ie have a different path).
It would mean that you could only have one script with the same as a registered task, but that does not seem too burdensome to me.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

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

21 Oct 2016, 18:06

evilC wrote: It looks like you should be able to call GetTasks which will give you an IRegisteredTaskCollection.
Any ideas on how to enumerate that?
I guess you could search for any tasks that match the pattern [RunAsTask] <ScriptName> but have a different number after the @ (ie have a different path).
It would mean that you could only have one script with the same as a registered task, but that does not seem too burdensome to me.

Code: Select all

;Thanks to HotKeyIt and Lexikos: https://autohotkey.com/boards/viewtopic.php?t=15316
if (!A_IsAdmin) {
	Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%",, UseErrorLevel
	ExitApp
}

TaskSchd  := ComObjCreate( "Schedule.Service" ),    TaskSchd.Connect()
    , TaskRoot  := TaskSchd.GetFolder( "\" )

for task in TaskRoot.GetTasks(0) {
	;MsgBox % task.Name
	if (task.Name == "Vrai reconnait vrai")
		TaskRoot.DeleteTask(task.Name, 0)
}
I can't say I know the rules of COM reference counting, esp. with IDispatch interfaces, so I could be doing something egregious not like releasing an object when I'm supposed to. In that case, sorry...

(Also, in the task XML, I'd add <Priority>4</Priority> after ExecutionTimeLimit>PT0S</ExecutionTimeLimit>. Or just as when you add a task manually to the Task Scheduler, your script will be running with Below Normal CPU priority and Low I/O priority...)
walfredo
Posts: 21
Joined: 22 May 2014, 19:32

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

26 Oct 2017, 13:51

This is driving me crazy. I'm trying to use this function to allow non-admin users to run WMIC in a console as an administrator. It works great for me-- I get the UAC prompt (just yes/no-- not a credentials prompt since I'm a domain admin) then on subsequent runs I am no longer prompted. For a standard user, when UAC prompts for username/password, I enter my credentials, and the script runs properly. On subsequent runs, the user still gets a UAC prompt for an admin username/password each time they run it. If I go look at the scheduled tasks for that user, there is no new entries. If I flip over to my profile on the same PC, there exists the scheduled task from the aforementioned first run. Am I confused about the purpose of this script?
walfredo
Posts: 21
Joined: 22 May 2014, 19:32

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

26 Oct 2017, 17:13

evilC wrote:I don't think that the intended use is to allow the script to run as a different user, it's intention is to elevate the logged-on user.
Thanks. I apologize for my inarticulateness-- I do want to elevate the logged-on user.
Standard, logged-on user doesn't have the privilege to run the command I'm wanting them to be able to run.
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

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

26 Oct 2017, 17:51

https://en.wikipedia.org/wiki/User_Account_Control
a user account may have administrator privileges assigned to it, but applications that the user runs do not inherit those privileges unless they are approved beforehand or the user explicitly authorizes it
Sounds more like you are trying to run as another account - I *think* (but I could be wrong) that RunAsTask is only able to allow the process to inherit the admin privileges of the logged-on user
walfredo
Posts: 21
Joined: 22 May 2014, 19:32

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

26 Oct 2017, 19:29

evilC wrote:https://en.wikipedia.org/wiki/User_Account_Control
a user account may have administrator privileges assigned to it, but applications that the user runs do not inherit those privileges unless they are approved beforehand or the user explicitly authorizes it
Sounds more like you are trying to run as another account - I *think* (but I could be wrong) that RunAsTask is only able to allow the process to inherit the admin privileges of the logged-on user
gotcha. So, literally just makes it so you only have to say "yes" once. Thanks again.
robodesign
Posts: 932
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

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

24 Apr 2018, 17:04

evilC wrote:I have been looking into why RunAsTask was not working for me in some instances with compiled scripts.

The problem seems to be with this line:
Run *RunAs %CmdLine%, %A_ScriptDir%, UseErrorLevel
If CmdLine contains "D:\Some Path\Some Script.exe" (The string contains quotes) then AHK tries to exectue Run, *RunAs "D:\Some Path\Some Script.exe" which fails with a "File not found" error. Instead the command-line should be Run, *RunAs D:\Some Path\Some Script.exe

Here is my temporary fix:

Code: Select all

  If ( not A_IsAdmin and not TaskExists )  {
	CLine       := ( A_IsCompiled ? A_ScriptFullpath  : """"  A_AhkPath """ """ A_ScriptFullpath """" )
    Run *RunAs %CLine%, %A_ScriptDir%, UseErrorLevel
	OutputDebug % "AHK| RunAsAdmin ErrorLevel is: " ErrorLevel ", A_LastError is: " A_LastError
	if (ErrorLevel != "ERROR")
		ExitApp
   }
Also, what's the deal with the statement "It is up to the user to clean and maintain the task scheduler."? I looked in the task scheduler and there were a couple of entries which appeared to be old and no longer needed - is this list potentially going to bloat and bloat over time?
Is it technically feasible to programatically maintain this list? ie for a given filename, ensure there is only ever one entry in the scheduler?

It looks like you should be able to call GetTasks which will give you an IRegisteredTaskCollection.
Any ideas on how to enumerate that?
I guess you could search for any tasks that match the pattern [RunAsTask] <ScriptName> but have a different number after the @ (ie have a different path).
It would mean that you could only have one script with the same as a registered task, but that does not seem too burdensome to me.
Any update on this "temporary fix" ? I am interested in implementing this solution for my compiled script as well.


Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

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

18 Apr 2020, 20:45

Hi,
Windows 10 x64 v1809
AHK 1.1.32.0 x64

I tried a simple hotkey script:

Code: Select all

RunAsTask()
F9::MsgBox
The first run after UAC confirmation works fine, the scheduler task is created, but any subsequent run does not work, the script exits immediately and the hotkey is not registered. Is this supposed to not work?
Thanks.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

19 Apr 2020, 05:08

TheHacker wrote:
18 Apr 2020, 20:45
hotkey is not registered. Is this supposed to not work?
Works fine In WIn 7 and Win 10.
How are you launching the script for the consecutive calls?
You shouldn't launch it from editor. The command line might change.

PS:
I have been using this with AHK v 1.1.15.03 for past several years without a flaw.
I'll go through the topic and give this function an update.
My Scripts and Functions: V1  V2
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

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

19 Apr 2020, 06:04

SKAN wrote:
19 Apr 2020, 05:08
How are you launching the script for the consecutive calls?
Simply clicking on test.ahk in Explorer. The "H" icon appears in the tray for a split-second and disappears again. #Persistent does not help. RunAsTask() is defined in %A_MyDocuments%\AutoHotkey\Lib\RunAsTask.ahk.
Thank you.

EDIT: I thought it was related to the hotkey for some reason, but neither does

Code: Select all

RunAsTask()
Run, regedit.exe
work. :eh:
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

23 Apr 2020, 16:45

TheHacker wrote:
19 Apr 2020, 06:04
SKAN wrote:
19 Apr 2020, 05:08
How are you launching the script for the consecutive calls?
Simply clicking on test.ahk in Explorer.
Ok! Does the full path to test.ahk have any space(s) in in it?
If yes, move test.ahk to root folder or any folder path that doesn't have spaces and try again.
I went through this topic and this flaw has been reported. I will fix it.
My Scripts and Functions: V1  V2
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

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

23 Apr 2020, 17:29

SKAN wrote:
23 Apr 2020, 16:45
Does the full path to test.ahk have any space(s) in in it?
If yes, move test.ahk to root folder or any folder path that doesn't have spaces and try again.
Nope, it does not. c:\test\test.ahk and the behavior is the same. First run works, consecutive runs show the icon for a split-second, then terminate.

Code: Select all

RunAsTask()
MsgBox
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

24 Apr 2020, 11:13

TheHacker wrote:
23 Apr 2020, 17:29
First run works, consecutive runs show the icon for a split-second, then terminate.

Code: Select all

RunAsTask()
MsgBox
Can you check the Task scheduler and confirm if the task was created?
If you right click on the task, you will see an option to export xml.
It will be very helpful if you can post the xml.

Thanks.
My Scripts and Functions: V1  V2
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

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

24 Apr 2020, 12:14

Sure, here you are:

Code: Select all

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.6" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <URI>\[RunAsTask] test.ahk @2836152222</URI>
  </RegistrationInfo>
  <Triggers />
  <Principals>
    <Principal id="Author">
      <UserId>*edited out by SKAN*</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>false</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Program Files\AutoHotkey\AutoHotkey.exe</Command>
      <Arguments>"C:\test\test.ahk"</Arguments>
      <WorkingDirectory>C:\test</WorkingDirectory>
    </Exec>
  </Actions>
</Task>
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

24 Apr 2020, 12:41

TheHacker wrote:
24 Apr 2020, 12:14
Sure, here you are:

Code: Select all

      <Command>C:\Program Files\AutoHotkey\AutoHotkey.exe</Command>
      <Arguments>"C:\test\test.ahk"</Arguments>
      <WorkingDirectory>C:\test</WorkingDirectory>
My A_AhkPath is D:\AutoHotkey\AutoHotkey.exe
Yours should be "C:\Program Files\AutoHotkey\AutoHotkey.exe", that is within double-quotes as there is a space in path.

A silly but costly mistake of mine :oops:
Code fixed.
Many thanks for your wonderful cooperation. :thumbup:
My Scripts and Functions: V1  V2

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 85 guests