Skip to content

Instantly share code, notes, and snippets.

@ddhahn
Last active December 12, 2015 02:38
Show Gist options
  • Save ddhahn/4700115 to your computer and use it in GitHub Desktop.
Save ddhahn/4700115 to your computer and use it in GitHub Desktop.
Envoke UAC to elevate a command
#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=elevate_this.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;Run a script using shellexecute runas. This will run the application/script
;at the highest available privilege level. Depending on UAC settings, this may
;cause a UAC prompt.
;Examples:
;elevate_this.exe c:\windows\system32\cscript.exe script.vbs /q /w /r /s
; THis would end up running wscript.exe with the arguments script.vbs /q /w /r /s
;+--- construct the new command line arguments so that we can preserve any quotes used in parameters passed to it
;+--- if $cmdline[1] begins with a double quote, we need to add three to the length $cmdline[1] to account for the extra
;+--- double quote.
if stringleft($cmdlineraw,1) = '"' Then
$newargs = stringmid($cmdlineraw,stringlen($cmdline[1])+3)
Else
$newargs = stringmid($cmdlineraw,stringlen($cmdline[1])+2)
EndIf
;+--- if we have a command line parameter, pass it. If not, don't.
if $cmdline[0] = 1 Then
exit(ShellExecuteWait('"' & $cmdline[1] & '"',"","","runas"))
Else
exit(ShellExecuteWait('"' & $cmdline[1] & '"',$newargs,"","runas"))
endIf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment