如何检查当前批处理脚本是否具有管理权限?
我知道如何使它调用自己与runas,但不知道如何检查管理权限。我所见过的唯一解决方案是粗糙的黑客工作或使用外部程序。好吧,其实我不在乎这是不是一份苦差事,只要它能在Windows XP或更新版本上运行就行。
如何检查当前批处理脚本是否具有管理权限?
我知道如何使它调用自己与runas,但不知道如何检查管理权限。我所见过的唯一解决方案是粗糙的黑客工作或使用外部程序。好吧,其实我不在乎这是不是一份苦差事,只要它能在Windows XP或更新版本上运行就行。
当前回答
不仅检查,而且自动获得管理权限 也就是win7/8/8.1 ff的自动UAC。下面是一个非常酷的程序,它还有一个特性:这个批处理代码片段不仅检查管理权限,而且会自动获取它们!(以及之前的测试,如果生活在一个支持UAC的操作系统上。)
有了这个技巧,你不需要更长的时间右击你的批处理文件“与管理权限”。如果你忘记了,从提升权限开始,UAC自动出现!此外,首先它是测试,如果操作系统需要/提供UAC,所以它表现正确,例如Win 2000/XP,直到Win 8.1测试。
@echo off
REM Quick test for Windows generation: UAC aware or not ; all OS before NT4 ignored for simplicity
SET NewOSWith_UAC=YES
VER | FINDSTR /IL "5." > NUL
IF %ERRORLEVEL% == 0 SET NewOSWith_UAC=NO
VER | FINDSTR /IL "4." > NUL
IF %ERRORLEVEL% == 0 SET NewOSWith_UAC=NO
REM Test if Admin
CALL NET SESSION >nul 2>&1
IF NOT %ERRORLEVEL% == 0 (
if /i "%NewOSWith_UAC%"=="YES" (
rem Start batch again with UAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
)
rem Program will now start again automatically with admin rights!
rem pause
goto :eof
)
该代码片段将一些好的批处理模式合并在一起,特别是(1)本Hooper在这个线程中的管理测试和(2)在BatchGotAdmin上读取的UAC激活,并由robvanderwoude在批处理站点上引用(respect)。(3)对于“VER | FINDSTR模式”的操作系统标识,我只是没有找到参考。)
(关于一些非常小的限制,当“NET SESSION”不工作时,如另一个答案所述-请随意插入另一个这些命令。对我来说,运行在Windows安全模式或特殊标准服务,这不是一个重要的用例-对一些管理员来说可能是。)
其他回答
whoami /groups | find "S-1-16-12288" > nul
if not errorlevel 1 (
echo ... connected as admin
)
另一种方法。
REM # # # # CHECKING OR IS STARTED AS ADMINISTRATOR # # # # #
FSUTIL | findstr /I "volume" > nul&if not errorlevel 1 goto Administrator_OK
cls
echo *******************************************************
echo *** R U N A S A D M I N I S T R A T O R ***
echo *******************************************************
echo.
echo.
echo Call up just as the Administrator. Abbreviation can be done to the script and set:
echo.
echo Shortcut ^> Advanced ^> Run as Administrator
echo.
echo.
echo Alternatively, a single run "Run as Administrator"
echo or in the Schedule tasks with highest privileges
pause > nul
goto:eof
:Administrator_OK
REM Some next lines code ...
一些服务器禁用命令“net session”所需的服务。 这导致管理检查总是说您没有管理权限,而您可能有。
问题
blak3r / Rushyo的解决方案适用于除Windows 8以外的所有系统。在Windows 8上运行AT会导致:
The AT command has been deprecated. Please use schtasks.exe instead.
The request is not supported.
(参见截图#1),将返回%errorLevel% 1。
研究
所以,我开始寻找其他需要更高权限的命令。rationallyparanoid网站上列出了一些这样的命令,所以我在当前Windows操作系统的两个极端(XP和8)上运行了每个命令,希望找到一个在两种操作系统上使用标准权限时都被拒绝访问的命令。
最终,我找到了一个- NET SESSION。一个真正的、干净的、普遍的解决方案,不包括:
在安全位置上创建数据或与数据交互 分析FOR循环返回的数据 搜索“管理员”字符串 使用AT (Windows 8不兼容)或WHOAMI (Windows XP不兼容)。
每一个都有自己的安全性、可用性和可移植性问题。
测试
我已经独立确认,这适用于:
Windows XP, x86 Windows XP, x64 Windows Vista, x86 Windows Vista, x64 Windows 7, x86 Windows 7, x64 Windows 8, x86 Windows 8, x64 Windows 10 v1909, x64
(见截图#2)
实施/使用
所以,要使用这个解决方案,只需像这样做:
@echo off
goto check_Permissions
:check_Permissions
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
) else (
echo Failure: Current permissions inadequate.
)
pause >nul
解释
NET SESSION是一个用于“管理服务器计算机连接”的标准命令。不带参数使用时,[它]显示与本地计算机的所有会话信息。”
所以,这是我给出的实现的基本过程:
@echo off Disable displaying of commands goto check_Permissions Jump to the :check_Permissions code block net session >nul 2>&1 Run command Hide visual output of command by Redirecting the standard output (numeric handle 1 / STDOUT) stream to nul Redirecting the standard error output stream (numeric handle 2 / STDERR) to the same destination as numeric handle 1 if %errorLevel% == 0 If the value of the exit code (%errorLevel%) is 0 then this means that no errors have occurred and, therefore, the immediate previous command ran successfully else If the value of the exit code (%errorLevel%) is not 0 then this means that errors have occurred and, therefore, the immediate previous command ran unsuccessfully The code between the respective parenthesis will be executed depending on which criteria is met
截图
Windows 8 AT %errorLevel%:
NET SESSION在Windows XP x86 - Windows 8 x64上:
谢谢你,@Tilka,把你接受的答案改成了我的。:)
替代方案:使用专门为此目的设计的外部实用程序,例如IsAdmin.exe(不受限制的免费软件)。
退出代码:
0 -当前用户不是Administrators组的成员
1 -管理员的当前用户成员和运行提升
2 -管理员的当前用户成员,但未运行提升