Program Elevator: resurrected script.
Posted: 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.
Seems to work okay, please let me know if not.
Regards,
burque505
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.
Seems to work okay, please let me know if not.
Code: Select all
; Program Elevator
; AutoHotkey Version: 1.1.32.00
; 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)"))
{
try
{
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
#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!
Return
}
GuiControlGet, File, , File2Elevate
If Not File
{ MsgBox Please enter a File to elevate!
return
}
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
Else
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")
objService.Connect()
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%
ExitApp
Return ; -*
GuiClose: ; *
ExitApp ; -*
burque505