One of the things I noticed is the hybrid VBSsript - batch approach used in several threads to run commands elevated.
Here is a pure batch way to accomplish the same:
Code: Select all
@echo off :: Create dummy script set RANDOM=>"%temp%\%~n0.ELEVATE" :: File association: Run .ELEVATE always elevated reg add HKCU\Software\Classes\.elevate /ve /D MaartenElevate /F >nul reg add HKCU\Software\Classes\MaartenElevate\Shell\runas\command /ve /D "cmd.exe /c pushd \"%%w\" & start \"%~n0\" %%*" /F >nul :: Execute dummy .ELEVATE script "%temp%\%~n0.ELEVATE" %* :: Clean up reg delete HKCU\Software\Classes\.elevate /F >nul reg delete HKCU\Software\Classes\MaartenElevate /F >nul del /Q "%temp%\%~n0.ELEVATE"
You can run internal commands as well as external commands. E.g. both SUDO dir or SUDO notepad dummy.txt will work.
How this works
- Create a dummy file extension (.elevate)
- Associate this file extension with *one* verb: RunAs.
RunAs is Windows' predefined verb used to elevate.
When there is no default verb defined, it will open the alphabetically first verb. Which implies that with one verb defined, that one will be used (I did my own groundwork research )
- That verb (RunAs) will cause this command to execute (elevated because of the RunAs):
"cmd.exe /c pushd \"%%w\" & start \"%~n0\" %%*"
- %%w is a Windows shell variable passed to the verb. it is the current working directory
- %%* are the SUDO parameters passed through to CMD
- \"%~n0\" ("SUDO") is only used as the window title.
- Start a dummy .elevate file
- This will trigger the RunAs routine, causing elevation.
To summarize: a command like SUDO notepad "C:\Windows\System32\drivers\etc\hosts", started from C:\temp will (elevated) run:
cmd.exe /c pushd "C:\Temp" & start "SUDO" notepad "C:\Windows\System32\drivers\etc\hosts"
It shouldn't be too hard to integrate this method in a script that needs elevation.
It is more SU that SUDO, but I liked that name better ...