RunAsTask() - Auto-elevates script without UAC prompt

Post your working scripts, libraries and tools
TheHacker
Posts: 28
Joined: 24 Apr 2017, 07:53

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

24 Apr 2020, 13:19

SKAN wrote:
24 Apr 2020, 12:41
Code fixed.
Many thanks for your wonderful cooperation. :thumbup:
Thank you. I have deleted all the Task Scheduler entries and replaced the function code, however, the same problem persists. Works the first time but the second time only the H icon shows up for a moment.
If there was a problem in the path to AutoHotkey.exe, the H icon wouldn't even show up since Windows wouldn't even be able to find and start AutoHotkey.exe, would it?
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

24 Apr 2020, 13:23

Okay does the quotes now appear around the path?
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

24 Apr 2020, 13:35

TheHacker wrote:
24 Apr 2020, 13:19
If there was a problem in the path to AutoHotkey.exe, the H icon wouldn't even show up since Windows wouldn't even be able to find and start AutoHotkey.exe, would it?
AutoHotkey.exe is started twice.
First time is un-elevated which requests task scheduler to run it "elevated" and immediately exits.
The flickering you see is the first "un-elevated" instance.
Task scheduler will never start your AutoHotkey without a quoted path.
I had tested and confirmed before announcing 'code fixed'.
TheHacker
Posts: 28
Joined: 24 Apr 2017, 07:53

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

24 Apr 2020, 13:56

SKAN wrote:
24 Apr 2020, 13:23
Okay does the quotes now appear around the path?
Yes, they do. I added them manually before I deleted all the old entries just for a test and it didn't work either. Here is the current .xml:

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>???</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>
SKAN wrote:
24 Apr 2020, 13:35
AutoHotkey.exe is started twice.
First time is un-elevated which requests task scheduler to run it "elevated" and immediately exits.
The flickering you see is the first "un-elevated" instance.
I see, makes sense, thank you. That also explains why when I go into Task Scheduler, select the task and click Run, nothing happens.
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

24 Apr 2020, 14:21

TheHacker wrote:
24 Apr 2020, 13:56
I think I see two anomalies in your xml.
You user id (I have edited it out in both your posts) appears as SID instead of a proper name.

Then the following line:

Code: Select all

<UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
RunAsTask() sets it to default value which is false.
It appears as true in your xml. I don't know what that setting is for and why your OS is overriding it.
TheHacker
Posts: 28
Joined: 24 Apr 2017, 07:53

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

24 Apr 2020, 14:39

With some further testing I found out that using AutoHotkeyU64_UIA.exe (AutoHotkey.exe is a copy of this file) is the problem. If I use AutoHotkeyU64.exe instead it works just fine.
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

24 Apr 2020, 15:07

TheHacker wrote:
24 Apr 2020, 14:39
If I use AutoHotkeyU64.exe instead it works just fine.
:D :thumbup:
TheHacker
Posts: 28
Joined: 24 Apr 2017, 07:53

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

24 Apr 2020, 15:24

So I should be fine if I modify line 59 as follows:

Code: Select all

      <Command>""" ( A_IsCompiled ? A_ScriptFullpath : SubStr(A_AhkPath, 1, InStr(A_AhkPath, "\", , -1)) . "AutoHotkeyU64.exe" ) """</Command>
Any idea why it does not work with the UIA version?
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

24 Apr 2020, 15:49

TheHacker wrote:
24 Apr 2020, 15:24
Any idea why it does not work with the UIA version?
Never tried UIA. It is not compatible with my installation path D:\AutoHotkey
Search the forum for "UIA". I find some.. but not sure if it is related.
User avatar
hasantr
Posts: 592
Joined: 05 Apr 2016, 14:18
Location: İstanbul

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

13 May 2020, 18:51

This is a very good job, SKAN. Thanks for providing us.
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

15 May 2020, 15:21

hasantr wrote:
13 May 2020, 18:51
This is a very good job, SKAN. Thanks for providing us.
Thank you. I will update the code soon. :thumbup:

Meanwhile: I would like to know if anyone interested in seeing a custom implementation of #SingleInstance and support for command line parameters.
User avatar
hasantr
Posts: 592
Joined: 05 Apr 2016, 14:18
Location: İstanbul

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

15 May 2020, 15:24

SKAN wrote:
15 May 2020, 15:21
hasantr wrote:
13 May 2020, 18:51
This is a very good job, SKAN. Thanks for providing us.
Thank you. I will update the code soon. :thumbup:

Meanwhile: I would like to know if anyone interested in seeing a custom implementation of #SingleInstance and support for command line parameters.
Thanks. :bravo:
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

15 May 2020, 15:27

@hasantr Sorry for what?

Edited:
hasantr wrote: Thanks. :bravo:
Ah! you edited it out. :)
User avatar
hasantr
Posts: 592
Joined: 05 Apr 2016, 14:18
Location: İstanbul

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

15 May 2020, 15:27

SKAN wrote:
15 May 2020, 15:27
@hasantr Sorry for what?
I wrote in the wrong place. :)
User avatar
Cerberus
Posts: 169
Joined: 12 Jan 2016, 15:46

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

16 May 2020, 08:58

Support for command-line parameters sounds interesting?
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

16 May 2020, 09:47

Cerberus wrote:
16 May 2020, 08:58
Support for command-line parameters sounds interesting?
Yes.
Command-line support without writing anything to disk.
A custom reload will also pass on the parameters.

I need testers, who really need this feature.
I already have a working prototype.

Thank you.
williams
Posts: 31
Joined: 01 Jan 2015, 08:40

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

08 Sep 2020, 23:53

SKAN wrote:
16 May 2020, 09:47
Cerberus wrote:
16 May 2020, 08:58
Support for command-line parameters sounds interesting?
Yes.
Command-line support without writing anything to disk.
A custom reload will also pass on the parameters.

I need testers, who really need this feature.
I already have a working prototype.

Thank you.
I really need it, Did you complete it ?
williams
Posts: 31
Joined: 01 Jan 2015, 08:40

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

09 Sep 2020, 02:11

BUG Report
TaskName case sensitive,when drive letter is lowercase,the UAC prompt is forced to pop up

test1.ahk

Code: Select all

; The Drive letter is lowercase 'd',Test2 pops up the UAC prompt every time
Run, d:\AHKS32.exe "d:\test2.ahk"
test2.ahk

Code: Select all

#SingleInstance force

; do nothing
MsgBox, test2

RunAsTask()
RunAsTask() {                         ;  By SKAN,  http goo.gl /yG6A1F,  Broken Link for safety  CD:19/Aug/2014 | MD:22/Aug/2014

  Local CmdLine, TaskName, TaskExists, XML, TaskSchd, TaskRoot, RunAsTask
  Local TASK_CREATE := 0x2,  TASK_LOGON_INTERACTIVE_TOKEN := 3 

  Try TaskSchd  := ComObjCreate( "Schedule.Service" ),    TaskSchd.Connect()
    , TaskRoot  := TaskSchd.GetFolder( "\" )
  Catch
      Return "", ErrorLevel := 1    
  
  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 )

  Try RunAsTask := TaskRoot.GetTask( TaskName )
  TaskExists    := ! A_LastError 


  If ( not A_IsAdmin and TaskExists )      { 

    RunAsTask.Run( "" )
    ExitApp

  }

  If ( not A_IsAdmin and not TaskExists )  { 

    Run *RunAs %CmdLine%, %A_ScriptDir%, UseErrorLevel
    ExitApp

  }

  If ( A_IsAdmin and not TaskExists )      {  

    XML := "
    ( LTrim Join
      <?xml version=""1.0"" ?><Task xmlns=""http schemas.microsoft.com /windows/2004/02/mit/task""><Regi  Broken Link for safety
      strationInfo /><Triggers /><Principals><Principal id=""Author""><LogonType>InteractiveToken</LogonT
      ype><RunLevel>HighestAvailable</RunLevel></Principal></Principals><Settings><MultipleInstancesPolic
      y>Parallel</MultipleInstancesPolicy><DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries><
      StopIfGoingOnBatteries>false</StopIfGoingOnBatteries><AllowHardTerminate>false</AllowHardTerminate>
      <StartWhenAvailable>false</StartWhenAvailable><RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAva
      ilable><IdleSettings><StopOnIdleEnd>true</StopOnIdleEnd><RestartOnIdle>false</RestartOnIdle></IdleS
      ettings><AllowStartOnDemand>true</AllowStartOnDemand><Enabled>true</Enabled><Hidden>false</Hidden><
      RunOnlyIfIdle>false</RunOnlyIfIdle><DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteApp
      Session><UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine><WakeToRun>false</WakeToRun><
      ExecutionTimeLimit>PT0S</ExecutionTimeLimit></Settings><Actions Context=""Author""><Exec>
      <Command>"   (  A_IsCompiled ? A_ScriptFullpath : A_AhkPath )       "</Command>
      <Arguments>" ( !A_IsCompiled ? """" A_ScriptFullpath  """" : "" )   "</Arguments>
      <WorkingDirectory>" A_ScriptDir "</WorkingDirectory></Exec></Actions></Task>
    )"    

    TaskRoot.RegisterTask( TaskName, XML, TASK_CREATE, "", "", TASK_LOGON_INTERACTIVE_TOKEN )

  }         

Return TaskName, ErrorLevel := 0
} ; _____________________________________________________________________________________________________
User avatar
SKAN
Posts: 807
Joined: 29 Sep 2013, 16:58

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

09 Sep 2020, 05:27

williams wrote:
SKAN wrote: Command-line support without writing anything to disk.
A custom reload will also pass on the parameters.

I need testers, who really need this feature.
I already have a working prototype.
I really need it, Did you complete it ?
No takers, so I didn't.
It would be lying around some where.
Allow me some time. It is a complicated script.
 
 
williams wrote: BUG Report
TaskName case sensitive,when drive letter is lowercase,the UAC prompt is forced to pop up
 
I will check and fix. Thanks for reporting. :thumbup:
williams
Posts: 31
Joined: 01 Jan 2015, 08:40

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

09 Sep 2020, 11:02

SKAN wrote:
09 Sep 2020, 05:27
No takers, so I didn't.
It would be lying around some where.
Allow me some time. It is a complicated script.
 
Thanks
Don't know how to be a tester, but I'll use your code in my scripts, if they don’t work properly, I’ll report them
:bravo:

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 20 guests