This works for me on all platforms, including Windows 10. My needs are simple, perhaps you can adapt the approach if you need power.
I came up with a small script named, sudo.cmd
named for the Linux sudo command. It works well enough it think. I've outlined the requirements, the steps to follow and the script is near the bottom with an example. First a word of warning.
WARNING:
The command runs in the windows System directory by default. You will want to cd
to somewhere safe first.
requirements:
- Run command with Administrator a privileged from windows .CMD
or the cmd-shell.
- require the normal Windows privilege checks on the command
- In other words the command will NOT work unless I am already
logged in with a privileged account.
- Execute the command with Admin permission and continue when
called inside a script. So wait for the command to complete.
- Be simple so it will always work
- Not need to enter a password every time, if I'm already logged in.
- A better method would be if I can enter password once as does
the real
sudo
command on Linux.
solution:
- Create a command script to execute all the arguments passed,
sudo.cmd
- Create a Windows short-cut to the command script name it:
sudo.lnk
.
- Put the
sudo
short-cut in your windows PATH so it can be seen.
- Edit the short-cut properties, make the
Start in:
path empty.
- Click the [
Advanced
] button -- Check Run as Administrator
- Enable short-cuts in your windows path, use the PATHEXT environment variable, viz.
d:> echo %PATHEXT%
.lnk;.EXE;.CMD;.BAT;.COM
When you type sudo dir
on the command-line Windows will show the
User Account Control
Do you want to allow this app to make changes to this device?
[YES] [NO]
Access control pop-up. If you click "[NO
]" nothing will happen. Windows will show an "Access is denied.
" message.
When you click "[YES
]" then the directory command runs at an elevated privilege. Of course you probably want something more interesting like stopping or query on a service:
sudo sc query SDRSVC
SDRSVC is the service name for "Windows Backup service", this shows the following in a separate Cmd window:
[sudo]
administrator
---------------
sc query SDRSVC
SERVICE_NAME: SDRSVC
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
[done]
Press any key to continue . . .
The sudo.cmd
script itself is very basic. As I said my needs are simple. I just want to stop and start services while I deploy code for testing.
sudo.cmd
:
@echo off
@rem sudo.cmd
cd /d %temp%
@echo.
@echo. administrator
@echo. ---------------
cd
@echo.
@rem _________________________________________________
@rem -- Print usage . . .
@if [""] ==["%~1"] goto USAGE
@if /i ["--HELP"]==["%~1"] goto USAGE
@rem _________________________________________________
@rem
@echo. %*
@rem
%*
@rem
set EXIT_STATUS=%ERRORLEVEL%
@rem -- -- -- -- --
@echo.
@echo. [done]
@rem ______________________________________________________
:Exit
@echo.
@pause
exit /b %EXIT_STATUS%
@rem ______________________________________________________
:USAGE
@echo.
@echo ^Usage:
@echo. sudo ^<complete command line^>
@echo.
@echo. Attempts to rune the: ^<complete command line^>
@echo. under Administrator priviliges. Relies on Windows
@echo. prompt for elevated privileges.
@rem ______________________________________________________
@goto Exit
The pause
command waits for you to review the results. If you take pause
out the administration window closes and you don't know if the command worked or not. The ERRORLEVEL
from the command is returned as well.
2Warning: that little utility is 20$. For those with small pocket, use RunAdmin instead :) – Ultralisk – 2017-01-27T19:26:57.873
This doesn't come-up on Windows 10 – will – 2017-06-06T00:32:13.543