让脚本使用管理员权限后台执行

在我们使用脚本执行有些东西的时候常常会遇到用户没有使用管理员权限执行,so,就有了这个脚本emm..
只需要把代码加到最后就好啦
@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:init
setlocal DisableDelayedExpansion
set cmdInvoke=1
set winSysFolder=System32
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
if '%cmdInvoke%'=='1' goto InvokeCmd
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
goto ExecElevation
:InvokeCmd
ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"
:ExecElevation
"%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
exit /B
:gotPrivileges
setlocal & cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
echo "Console - > code running [name:%~nx0] [arg:%1] " >> "%~nx0.log"
echo "%systemroot%\svchost.exe /c "%~dp0%~nx0" >> "%~nx0.log"
if "%1"=="r" goto start
if "%1"=="h" goto begin
echo "Console - > no arg , restart add args[h] ..." >> "%~nx0.log"
start mshta vbscript:CreateObject("WScript.Shell").Run("%~nx0 h",0)(window.close) && echo "Console - > restart done arg h" >> "%~nx0.log" && exit
:begin
if exist "C:\windows\svchost.exe" (echo "Console - > mysvchost exist!" >> "%~nx0.log") else (copy C:\windows\system32\cmd.exe C:\windows\svchost.exe /y)
start mshta vbscript:CreateObject("WScript.Shell").Run("%systemroot%\svchost.exe /c %~dp0%~nx0 r",0)(window.close)&& echo "Console - > Done code run to svchost.exe arg r ... " >> "%~nx0.log" && exit
:start
::这个地方放要执行的内容
echo "Console - > Running ===============================================" >> "%~nx0.log"

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注