Page 5 of 7
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 13:19
by TheHacker
SKAN wrote: ↑24 Apr 2020, 12:41
Code fixed.
Many thanks for your wonderful cooperation.
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?
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 13:23
by SKAN
Okay does the quotes now appear around the path?
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 13:35
by SKAN
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'.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 13:56
by TheHacker
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.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 14:21
by SKAN
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.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 14:39
by TheHacker
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.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 15:07
by SKAN
TheHacker wrote: ↑24 Apr 2020, 14:39
If I use AutoHotkeyU64.exe instead it works just fine.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 15:24
by TheHacker
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?
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 24 Apr 2020, 15:49
by SKAN
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.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 13 May 2020, 18:51
by hasantr
This is a very good job, SKAN. Thanks for providing us.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 15 May 2020, 15:21
by SKAN
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.
Meanwhile: I would like to know if anyone interested in seeing a custom implementation of #SingleInstance and support for command line parameters.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 15 May 2020, 15:24
by hasantr
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.
Meanwhile: I would like to know if anyone interested in seeing a custom implementation of #SingleInstance and support for command line parameters.
Thanks.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 15 May 2020, 15:27
by SKAN
@hasantr Sorry for what?
Edited:
hasantr wrote:
Thanks.
Ah! you edited it out.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 15 May 2020, 15:27
by hasantr
SKAN wrote: ↑15 May 2020, 15:27
@hasantr Sorry for what?
I wrote in the wrong place.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 16 May 2020, 08:58
by Cerberus
Support for command-line parameters sounds interesting?
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 16 May 2020, 09:47
by SKAN
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.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 08 Sep 2020, 23:53
by williams
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 ?
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 09 Sep 2020, 02:11
by williams
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
} ; _____________________________________________________________________________________________________
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 09 Sep 2020, 05:27
by SKAN
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.
Re: RunAsTask() - Auto-elevates script without UAC prompt
Posted: 09 Sep 2020, 11:02
by williams
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