批处理脚本:如何检查管理员权限 [英] Batch script: how to check for admin rights
问题描述
我如何检查当前批处理脚本具有管理员权限?
我知道如何使它自称与运行方式,而不是去检查管理权限。我见过的唯一的解决方案是原油破解工作或使用外部程序。嗯,其实我不在乎,如果它是一个黑客的工作,只要它适用于Windows XP和更新的。
问题
<一个href=\"http://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights#8995407\">blak3r / <一个href=\"http://stackoverflow.com/questions/7985755/how-to-detect-if-cmd-is-running-as-administrator-has-elevated-privileges#7986021\">Rushyo's解决方案正常工作的一切,除了Windows 8中运行 AT
在Windows 8的结果:
的AT命令已经去precated。请改用SchTasks.exe会。不支持该请求。
(见截图#1),将返回%ERRORLEVEL%
1
。
&NBSP;
研究
所以,我去寻找需要提升权限的其他指令。 <一href=\"http://www.rationallyparanoid.com/articles/windows-security-commands.html\">rationallyparanoid.com有一些名单,所以我目前的Windows操作系统的两个相反的极端运行每个命令(XP和8)在寻找与标准权限运行,将被拒绝在两个操作系统访问的命令的希望。
最后,我没有找到一个 - NET SESSION
。 A 真正的,干净的,通用的解决方案,不涉及:
- 创建或交互与在安全位置的数据
- 分析从
返回数据
循环 - 搜索字符串管理员
- 使用
AT
(Windows 8的不兼容)或WHOAMI
(Windows XP不兼容)。
每个其中有自己的安全性,可用性和便携性的问题。
&NBSP;
测试
我已经独立确认这适用于:
- Windows XP中,86
- Windows XP中,64
- Windows Vista中,86
- Windows Vista中,64
- Windows 7中,86
- Windows 7中,64
- Windows 8中,86
- Windows 8中,64
(见截图2#)
&NBSP;
实施/用法
因此,要使用此解决方案,只需做这样的事情:
关闭@echo
转到check_Permissions:check_Permissions
呼应所需的管理权限。检测权限... 净会议&GT; NUL 2 - ;&放大器; 1
如果%ERRORLEVEL%= = 0(
回声成功:管理权限确定。
)其他(
回声失败:当前权限不足。
) 暂停&GT; NUL
可在这里,如果你懒:<一href=\"https://dl.dropbox.com/u/27573003/Distribution/Binaries/check_Permissions.bat\">https://dl.dropbox.com/u/27573003/Distribution/Binaries/check_Permissions.bat
&NBSP;
说明
NET SESSION
是用于标准命令管理服务器电脑的连接。如果不使用参数,[它]显示与本地计算机的所有会话的信息。
所以,这是我定实现的基本过程:
-
关闭@echo
- 禁用显示命令
-
转到check_Permissions
- 跳转到
:check_Permissions
code座
- 跳转到
-
净会议&GT; NUL 2 - ;&放大器; 1
- 运行命令
- 隐藏视觉输出命令
- 重定向标准输出(数字手柄1 /
STDOUT
)流NUL
- 重定向标准错误输出流(数字手柄2 /
STDERR
)到同一目的地的数字手柄1
- 重定向标准输出(数字手柄1 /
-
如果%ERRORLEVEL%= = 0
- 如果退出code的值(
%ERRORLEVEL%
)的是0
那么这意味着没有发生错误,因此,眼前的previous命令运行的成功
- 如果退出code的值(
-
其他
- 如果退出code的值(
%ERRORLEVEL%
)的不是0
那么这意味着错误发生,因此,眼前的previous命令运行的失败
- 如果退出code的值(
- 相应的括号之间的code将取决于哪个标准执行满足
&NBSP;
截图
&NBSP;
NET SESSION
在Windows XP 86 - Windows 8的x64的
&NBSP;
感谢您,@Tilka,为改变你接受的回答我的。 :)
How do I check if the current batch script has admin rights?
I know how to make it call itself with runas but not how to check for admin rights. The only solutions I've seen are crude hack jobs or use external programs. Well, actually I don't care if it is a hack job as long as it works on Windows XP and newer.
Issues
blak3r / Rushyo's solution works fine for everything except Windows 8. Running AT
on Windows 8 results in:
The AT command has been deprecated. Please use schtasks.exe instead.
The request is not supported.
(see screenshot #1) and will return %errorLevel%
1
.
Research
So, I went searching for other commands that require elevated permissions. rationallyparanoid.com had a list of a few, so I ran each command on the two opposite extremes of current Windows OSs (XP and 8) in the hopes of finding a command that would be denied access on both OSs when run with standard permissions.
Eventually, I did find one - NET SESSION
. A true, clean, universal solution that doesn't involve:
- the creation of or interaction with data in secure locations
- analyzing data returned from
FOR
loops - searching strings for "Administrator"
- using
AT
(Windows 8 incompatible) orWHOAMI
(Windows XP incompatible).
Each of which have their own security, usability, and portability issues.
Testing
I've independently confirmed that this works on:
- Windows XP, x86
- Windows XP, x64
- Windows Vista, x86
- Windows Vista, x64
- Windows 7, x86
- Windows 7, x64
- Windows 8, x86
- Windows 8, x64
(see screenshot #2)
Implementation / Usage
So, to use this solution, simply do something like this:
@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
Available here, if you're lazy: https://dl.dropbox.com/u/27573003/Distribution/Binaries/check_Permissions.bat
Explanation
NET SESSION
is a standard command used to "manage server computer connections. Used without parameters, [it] displays information about all sessions with the local computer."
So, here's the basic process of my given implementation:
@echo off
- Disable displaying of commands
goto check_Permissions
- Jump to the
:check_Permissions
code block
- Jump to the
net session >nul 2>&1
- Run command
- Hide visual output of command by
- Redirecting the standard output (numeric handle 1 /
STDOUT
) stream tonul
- Redirecting the standard error output stream (numeric handle 2 /
STDERR
) to the same destination as numeric handle 1
- Redirecting the standard output (numeric handle 1 /
if %errorLevel% == 0
- If the value of the exit code (
%errorLevel%
) is0
then this means that no errors have occurred and, therefore, the immediate previous command ran successfully
- If the value of the exit code (
else
- If the value of the exit code (
%errorLevel%
) is not0
then this means that errors have occurred and, therefore, the immediate previous command ran unsuccessfully
- If the value of the exit code (
- The code between the respective parenthesis will be executed depending on which criteria is met
Screenshots
NET SESSION
on Windows XP x86 - Windows 8 x64:
Thank you, @Tilka, for changing your accepted answer to mine. :)
这篇关于批处理脚本:如何检查管理员权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!