How to get admin privilege in cmd for running sc.e

2019-09-23 02:04发布

I have created a batch file for starting a service using sc.exe. But it asks for admin privilege while running. Is there is any code that can be used in batch file to elevate cmd to admin level?

2条回答
做自己的国王
2楼-- · 2019-09-23 02:15

Press Windows Key + X and click Command Prompt (Admin). Click yes if the user control center ask for permission

查看更多
爷的心禁止访问
3楼-- · 2019-09-23 02:39

You can do this:

@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if %errorlevel% NEQ 0 (GOTO askAdmin)
GOTO gotAdmin


:askAdmin
::batch is being ran as normal user
echo I'm not an admin yet
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B


:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
::batch is being ran as admin
echo Now I'm an admin!
pause
查看更多
登录 后发表回答