SUDO for CMD

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Eureka!
Posts: 14
Joined: 25 Jul 2019 18:25

SUDO for CMD

#1 Post by Eureka! » 28 Jul 2019 13:15

Just discovered this forum a couple of days ago and I have to say that it contains a wealth on information and groundwork research on batch mechanics. Very interesting!

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:

SUDO.cmd

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"
If you start SUDO.cmd without parameters, it wll start an elevated CMD.exe. Working directory is the same as the originating CMD.
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
  1. Create a dummy file extension (.elevate)
  2. 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 ;))
  3. That verb (RunAs) will cause this command to execute (elevated because of the RunAs):
    "cmd.exe /c pushd \"%%w\" & start \"%~n0\" %%*"
    Where:
    • %%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.
  4. Start a dummy .elevate file
  5. 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.

Notes
It is more SU that SUDO, but I liked that name better ...
Last edited by Eureka! on 05 Aug 2019 12:10, edited 1 time in total.

bakemonogatari
Posts: 8
Joined: 08 Jul 2019 05:22

Re: Run elevated commands / programs / shell

#2 Post by bakemonogatari » 02 Aug 2019 11:34

thank you, it works very well.

Eureka!
Posts: 14
Joined: 25 Jul 2019 18:25

Re: Run elevated commands / programs / shell

#3 Post by Eureka! » 03 Aug 2019 10:22

Thank you for your feedback :thumbsup:
(I started to think the forum went dead ..)

Post Reply