自动获得管理员权限来运行 Batch 脚本

自动获得管理员权限来运行 Batch 脚本,:main 以下输入要运行的脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@echo off

pushd "%CD%"
CD /D "%~dp0"

cls


::管理员账户
:: whoami /user |find "-500" >nul 2>nul
::if not errorlevel 1 (goto AdministratorAccount) else goto OtherAccount


::管理员权限运行
set TempFile_Name=%SystemRoot%\System32\BatTestUACin_SysRt%Random%.batemp
echo %TempFile_Name% 1>nul

( echo "BAT Test UAC in Temp" >%TempFile_Name% ) 1>nul 2>nul

if exist %TempFile_Name% (
echo use administrator privilege
del /f /q %TempFile_Name%
goto main
) else (
echo not use administrator privilege
goto OtherAccount
)



:OtherAccount
%1 %2
ver|find "5.">nul&&goto :st
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :st","","runas",1)(window.close)&goto :eof

:st
copy "%~0" "%windir%\system32\"
goto :main



:main

:: Code Go Here!