Program Elevator: resurrected script.

Post your working scripts, libraries and tools
Posts: 1305
Joined: 22 Jan 2017, 19:37

Program Elevator: resurrected script.

19 Mar 2020, 14:51

I found Program Elevator from December 2010 by Jeff Main and made a couple of minor changes to it.
1) Changed the font to 9 points so it looks okay at 100% and 125% scaling.
2) Changed the *RunAs section of the code to match the current docs.
3) Got rid of an OS version check. Just know it isn't going to work on Win98, NT, or XP.

The Windows shortcut will be created in the script directory. Move it wherever you like.
Screenshot.PNG (94.05 KiB) Viewed 932 times
Seems to work okay, please let me know if not.

Code: Select all

    ; Program Elevator
    ; AutoHotkey Version:
    ; Language:       	English
    ; Platform:       	Windows
    ; Author:         	Jeff Main <[email protected]> modified by burque505 20200319
    ; Revision:			March 19, 2020
    ; Script Function:
    ;	This script creates an elevated UAC shortcut when executed.
    ;	Uses Task Scheduler 2.0 API included in Windows Vista and higher. Therefore, not compatible, or necessary for Win XP
    ;	Just follow steps in initial dialog to 1: Name shortcut/task, 2: choose the executable 
    ;	file to elevate, 3: set additional launch options, and 4: go!
    ; * Environment
    	#SingleInstance, Force
    ; Updated RunAs command from original to match the docs.
    	full_command_line := DllCall("GetCommandLine", "str")

		if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
				if A_IsCompiled
					Run *RunAs "%A_ScriptFullPath%" /restart
					Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    	#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
    	SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
    	SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
    	SetTitleMatchMode, 2
    	DetectHiddenText, On ; -*
    ; Create Gui *
    ; Version check removed. It works or it doesn't. No WinXP. Tested on Win7 and Win10.
    ; Font changed to 9 points to work without changes at 100% or 125% scaling.
    	GUI, Font, s11, Verdana  
    	GUI, Add, Text, x12 y10 cBlue, This task and shortcut can only be created`, and work`, while logged in as an administrator.`nIt cannot be created in a standard user account!
    	GUI, Font, s9, Verdana  
    	GUI, Add, Text, x12 y52 h20, 1: Name your shortcut:
    	GUI, Add, Edit, x170 y51 w200 h22 vShortcutName,
    		GUI, Add, Text, x12 y80 h20, 2: Select the file to execute with this shortcut:
    		GUI, Add, Button, x333 y77 w100 h22 gBrowseEXE, Browse...
    		GUI, Add, Edit, x30 y105 w718 h22 vFile2Elevate,
    	GUI, Add, Text, x12 y135 h20, 3: (Optional)
    	GUI, Add, Text, x30 y155 h20, Additional arguments (e.g. file to open using above executable)
    	GUI, Add, Edit, x30 y175 w610 h22 vFileArgs,
    	GUI, Add, Button, x648 y175 w100 h22 gBrowseArg, Browse...
    		GUI, Add, Text, x30 y200 h20, Startup folder
    		GUI, Add, Edit, x30 y220 w610 h22 vFileStart,
    		GUI, Add, Button, x648 y220 w100 h22 gBrowseStart, Browse...
    	GUI, Add, Text, x30 y245 h20, Path to icon file (.ico, .exe, .dll)
    	GUI, Add, Edit, x30 y265 w610 h22 vFileIcon,
    	GUI, Add, Button, x648 y265 w100 h22 gBrowseIcon, Browse...
    		GUI, Add, Text, x12 y302 h25, 4:
    		GUI, Add, Button, x30 y294 w718 h35 gGo, Create task and elevated shortcut
    	GUI, Show, xCenter y5 h341 w760 , Create Elevated Task Shortcut
    Return ; -*
    BrowseEXE: ; *
    	FileSelectFile, SelectedFile, 3, , Open a file, Scripts/Executables (*.ahk; *.exe)
    	GuiControl,, File2Elevate, %SelectedFile%
    Return ; -*
    BrowseArg: ; *
    	FileSelectFile, ArgFile, 3, , Open a file, All Documents (*.*)
    	GuiControl,, FileArgs, %ArgFile%
    Return ; -*
    BrowseStart: ; *
    	FileSelectFolder, SelectedFolder, , , Select Startup Folder, All Documents (*.*)
    	GuiControl,, FileStart, %SelectedFolder%
    Return ; -*
    BrowseIcon: ; *
    	FileSelectFile, SelectedIconFile, 3, , Open a file, Scripts/Executables (*.ico; *.exe; *.dll)
    	GuiControl,, FileIcon, %SelectedIconFile%
    Return ; -*
    Go: ;*
    	GuiControlGet, Name, , ShortcutName 
    	If Not Name 
    	{	MsgBox Please enter a shortcut name!
    	GuiControlGet, File, , File2Elevate 
    	If Not File
    	{	MsgBox Please enter a File to elevate!
    	GuiControlGet, Args, , FileArgs 
    	GuiControlGet, Start, , FileStart 
    	Path = SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree\Elevate\%Name%
    	RegRead,Exist,HKLM, %Path%, Id
    	If ErrorLevel ; Should error if no key exists, meaning no duplicate, so we can create the new task.
    		GoSub CreateTask
    		GoSub Duplicate
    Return ; -*
    Duplicate: ; *
    	msgbox, 0x60013, Title, Duplicate Task.`nWould you like to Overwrite/Update this task?
    	IfMsgBox No
    		Return ; allow for changes in task name
    	IfMsgBox Cancel
    		ExitApp	; else overwrite/update task. -*
    CreateTask: ; *
    	GUI, Destroy
    	SubFolder :=
    	IfInString, Name, \
    	{ 	StringGetPos, Position, Name, \,R1
    		Position := Position + 1
    		Len := StrLen(Name)
    		If (Len > Position)
    		{	StringLeft, SubFolder, Name, Position
    			stringRight, Name, Name, Len - Position
    	Task := "Elevate\" . SubFolder . Name
    	ActionTypeExec = 0  ; specifies an executable action. 
    	TaskCreateOrUpdate = 6 
    	Task_Runlevel_Highest = 1 
    	objService := ComObjCreate("Schedule.Service") 
    	objFolder := objService.GetFolder("\") ; Get task root folder
    	objTaskDefinition := objService.NewTask(0) 
    	principal := objTaskDefinition.Principal 
    	principal.LogonType := 1    ; Set the logon type to TASK_LOGON_PASSWORD 
    	principal.RunLevel := Task_Runlevel_Highest  ; Tasks will be run with the highest privileges. 
    	colTasks := objTaskDefinition.Triggers 
    	colActions := objTaskDefinition.Actions 
    	objAction := colActions.Create(ActionTypeExec) 
    	objAction.ID := Name 
    	objAction.Path := File
    	objAction.Arguments := Args
    	objAction.WorkingDirectory := Start
    	objInfo := objTaskDefinition.RegistrationInfo 
    	objInfo.Author := "jsmain - mods burque505" 
    	objInfo.Description := "Run program without UAC dialog." 
    	objSettings := objTaskDefinition.Settings 
    	objSettings.Enabled := True 
    	objSettings.Hidden := True 
    	objSettings.StartWhenAvailable := True 
    	objSettings.ExecutionTimeLimit := "PT0S"
    	objSettings.DisallowStartIfOnBatteries := False
    	objSettings.StopIfGoingOnBatteries := False
    	objFolder.RegisterTaskDefinition(Task, objTaskDefinition, TaskCreateOrUpdate , "", "", 3 ) 
    	; Task built, now create a shortcut here
    	Target = C:\Windows\System32\schtasks.exe
    	LinkFile = %A_ScriptDir%\%Name%.lnk
    	Working = 
    	RunArgs = /run /tn "Elevate\%SubFolder%%Name%"
    	Description := "Run " . Name . " as Administrator, without UAC prompt."
    	IconFile = %SelectedIconFile%
    	ShortcutKey = 
    	IconNum = 1
    	RunState = 7	
    	FileCreateShortcut, %Target%, %LinkFile%, %Working%, %RunArgs%, %Description%, %IconFile%, %ShortcutKey%, %IconNum%, %RunState%
    Return ; -*
    GuiClose: ; *
    	ExitApp ; -*
Posts: 1188
Joined: 16 Oct 2013, 13:53

Re: Program Elevator: resurrected script.

17 Apr 2020, 06:35

It seems to work for me. (Win 10)
Something I miss is being able to create a new or chose a library in the scheduler.
(Now all results from this program are placed under the Elevate directory)
Posts: 1305
Joined: 22 Jan 2017, 19:37

Re: Program Elevator: resurrected script.

17 Apr 2020, 08:50

@Albireo, that certainly sounds like a worthwhile addition. I'll work on it over the weekend.

EDIT: It appears you can both create and access subfolders, but for now they will be below 'Elevate' in the registry (I'll work on that). Here's a screenshot of creating a subfolder and task in that subfolder.
The file to execute doesn't appear in the screenshot, but I've checked this with both existing and non-existing subfolders (Win7 only so far).
Capture.PNG (80.31 KiB) Viewed 528 times
Here's what was created in the registry when I ran this (I added a file to execute, of course):
reg.PNG (3.03 KiB) Viewed 528 times

Posts: 1188
Joined: 16 Oct 2013, 13:53

Re: Program Elevator: resurrected script.

17 Apr 2020, 17:56

Yes, it can be an easy solution!

Have tested a few different configurations and it seems to work stable.
If the name in the scheduler already exists, the program asks what will happen (good)

One thing, I saw .: RegRead have a New Syntax (the old work, but how long?)

Maybe it´s possible to change the directory interactively - like this? (not tested)
Choice of directory in the scheduler?
However, it would be good to be able to choose directly from the GUI.

Have you any idea what Startup folder - means? / used for?
Should it point to the directory where the program to run is located?
Same as WorkingDir in AHK?

Would also be useful, be able to interactively enter a comment or specify a writer / responsible person.
If no comment or Author / creator is specified, it can be the same as today.

Posts: 1305
Joined: 22 Jan 2017, 19:37

Re: Program Elevator: resurrected script.

17 Apr 2020, 18:03

@Albireo, I think we're on the same page, good suggestions! Here's what I have at the moment. Needs @jballi's AddTooltip.ahk, also below.
More tomorrow :D
508.PNG (37.16 KiB) Viewed 490 times

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Rafaews and 84 guests