如何检查当前批处理脚本是否具有管理权限?

我知道如何使它调用自己与runas,但不知道如何检查管理权限。我所见过的唯一解决方案是粗糙的黑客工作或使用外部程序。好吧,其实我不在乎这是不是一份苦差事,只要它能在Windows XP或更新版本上运行就行。


当前回答

这里有另一个添加到列表中;-)

(尝试在系统位置创建文件)

CD.>"%SystemRoot%\System32\Drivers\etc\_"
MODE CON COLS=80 LINES=25

IF EXIST "%SystemRoot%\System32\Drivers\etc\_" (

  DEL "%SystemRoot%\System32\Drivers\etc\_"

  ECHO Has Admin privileges

) ELSE (

  ECHO No Admin privileges

)

MODE CON重新初始化屏幕,并在没有权限写入系统位置时压制任何文本/错误。

其他回答

这里有另一个添加到列表中;-)

(尝试在系统位置创建文件)

CD.>"%SystemRoot%\System32\Drivers\etc\_"
MODE CON COLS=80 LINES=25

IF EXIST "%SystemRoot%\System32\Drivers\etc\_" (

  DEL "%SystemRoot%\System32\Drivers\etc\_"

  ECHO Has Admin privileges

) ELSE (

  ECHO No Admin privileges

)

MODE CON重新初始化屏幕,并在没有权限写入系统位置时压制任何文本/错误。

编辑:版权已指出这是不可靠的。使用UAC批准读访问将允许dir成功。我有更多的脚本来提供另一种可能性,但它不是只读的。

reg query "HKLM\SOFTWARE\Foo" >NUL 2>NUL && goto :error_key_exists
reg add "HKLM\SOFTWARE\Foo" /f >NUL 2>NUL || goto :error_not_admin
reg delete "HKLM\SOFTWARE\Foo" /f >NUL 2>NUL || goto :error_failed_delete
goto :success

:error_failed_delete
  echo Error unable to delete test key
  exit /b 3
:error_key_exists
  echo Error test key exists
  exit /b 2
:error_not_admin
  echo Not admin
  exit /b 1
:success
  echo Am admin

旧答案如下

警告:不可靠


根据and31415提出的其他一些很好的答案和观点,我发现我是以下人的粉丝:

dir "%SystemRoot%\System32\config\DRIVERS" 2>nul >nul || echo Not Admin

依赖性少,速度快。

从字面上看,在SE上有几十个答案和相关的问题,所有这些都在这样或那样的方式上存在缺陷,清楚地表明Windows没有提供可靠的内置控制台实用程序。所以,是时候推出你自己的了。

下面的C代码,基于检测程序是否以完全管理员权限运行,在Win2k+1中工作,在任何地方和所有情况下(UAC,域,可传递组…)-因为它在检查权限时与系统本身相同。它用消息(可以用开关使其静音)和退出代码表示结果。

它只需要编译一次,然后你可以复制。exe到处-它只依赖于kernel32.dll和advapi32.dll(我已经上传了一个副本)。

chkadmin.c:

#include <malloc.h>
#include <stdio.h>
#include <windows.h>
#pragma comment (lib,"Advapi32.lib")

int main(int argc, char** argv) {
    BOOL quiet = FALSE;
    DWORD cbSid = SECURITY_MAX_SID_SIZE;
    PSID pSid = _alloca(cbSid);
    BOOL isAdmin;

    if (argc > 1) {
        if (!strcmp(argv[1],"/q")) quiet=TRUE;
        else if (!strcmp(argv[1],"/?")) {fprintf(stderr,"Usage: %s [/q]\n",argv[0]);return 0;}
    }

    if (!CreateWellKnownSid(WinBuiltinAdministratorsSid,NULL,pSid,&cbSid)) {
        fprintf(stderr,"CreateWellKnownSid: error %d\n",GetLastError());exit(-1);}

    if (!CheckTokenMembership(NULL,pSid,&isAdmin)) {
        fprintf(stderr,"CheckTokenMembership: error %d\n",GetLastError());exit(-1);}

    if (!quiet) puts(isAdmin ? "Admin" : "Non-admin");
    return !isAdmin;
}

1MSDN声称api是XP+,但这是错误的。CheckTokenMembership是2k+,另一个甚至更老。最后一个链接还包含一种更复杂的方法,即使在NT中也可以工作。

注意: 检查\system32\config\系统的调用 将总是失败在WOW64,(例如从%systemroot%\syswow64\cmd.exe / 32位Total Commander),所以脚本运行在32位shell在64位系统将永远循环… 更好的方法是检查预读目录的权限:

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\Prefetch\"

Win XP到7测试,但它失败在WinPE在windows 7安装。Wim没有这样的dir或cacls.exe

在winPE和wow64中,使用openfiles.exe检查失败:

OPENFILES > nul

在Windows 7中,它将errorlevel为“1”,信息为“目标系统需要是32位操作系统”

这两个检查也可能在恢复控制台失败。

在Windows XP - 8 32/64位,WOW64和WinPE中工作的是:目录创建测试(如果管理员没有对每个人的Windows目录进行毯式轰炸……)和

net session

and

reg add HKLM /F

检查。

还有一个注意在一些windows XP(和其他版本可能太,取决于管理员的修补)依赖于注册表项直接调用bat/cmd从.vbs脚本将失败的信息,bat/cmd文件不与任何相关…

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
cscript "%temp%\getadmin.vbs" //nologo

另一方面,用bat/cmd文件的参数调用cmd.exe可以正常工作:

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd.exe", "/C %~s0", "", "runas", 1 >> "%temp%\getadmin.vbs"
cscript "%temp%\getadmin.vbs" //nologo

更多的问题

正如@Lectrode所指出的,如果您试图在服务器服务停止时运行net session命令,您将收到以下错误消息:

The Server service is not started.

More help is available by typing NET HELPMSG 2114

在这种情况下,%errorLevel%变量将被设置为2。

注意:服务器服务在安全模式下(有无联网)不会启动。

寻找替代方案

的东西:

可以在Windows XP及更高版本(32位和64位)上运行; 不涉及注册表或任何系统文件/文件夹; 与系统区域无关的工作; 即使在安全模式下也能给出正确的结果。

于是我启动了一台普通的Windows XP虚拟机,开始在C:\Windows\System32文件夹中滚动应用程序列表,试图获得一些想法。经过反复试验,这是我想出的肮脏(双关语)方法:

fsutil dirty query %systemdrive% >nul

fsutil dirty命令需要管理员权限才能运行,否则将失败。“%systemdrive%”为环境变量,返回安装操作系统的盘符。输出被重定向为null,因此被忽略。%errorlevel%变量只有在成功执行时才会被设置为0。

以下是文档的内容:

Fsutil dirty Queries or sets a volume's dirty bit. When a volume's dirty bit is set, autochk automatically checks the volume for errors the next time the computer is restarted. Syntax fsutil dirty {query | set} <VolumePath> Parameters query Queries the specified volume's dirty bit. set Sets the specified volume's dirty bit. <VolumePath> Specifies the drive name followed by a colon or GUID. Remarks A volume's dirty bit indicates that the file system may be in an inconsistent state. The dirty bit can be set because: The volume is online and it has outstanding changes. Changes were made to the volume and the computer was shut down before the changes were committed to the disk. Corruption was detected on the volume. If the dirty bit is set when the computer restarts, chkdsk runs to verify the file system integrity and to attempt to fix any issues with the volume. Examples To query the dirty bit on drive C, type: fsutil dirty query C:

进一步的研究

虽然上面的解决方案从Windows XP开始工作,值得补充的是,Windows 2000和Windows PE(预安装环境)没有附带fsutil.exe,所以我们必须求助于其他东西。

在我之前的测试中,我注意到不带任何参数运行sfc命令会导致:

如果您没有足够的权限,则会出现错误; 可用参数及其用法的列表。

那就是:没有参数,没有聚会。这个想法是我们可以解析输出并检查我们是否得到了错误以外的任何东西:

sfc 2>&1 | find /i "/SCANNOW" >nul

错误输出首先被重定向到标准输出,然后通过管道传输到find命令。此时,我们必须寻找自Windows 2000以来所有Windows版本都支持的唯一参数:/SCANNOW。搜索是不区分大小写的,输出被重定向为nul而被丢弃。

以下是文档的节选:

香港证监会 扫描并验证所有受保护的系统文件的完整性,将错误的版本替换为正确的版本。 讲话 您必须作为Administrators组的成员登录才能运行sfc.exe。

示例使用

下面是一些粘贴后运行的例子:

Windows XP及以上版本

@echo off

call :isAdmin
if %errorlevel% == 0 (
echo Running with admin rights.
) else (
echo Error: Access denied.
)

pause >nul
exit /b

:isAdmin
fsutil dirty query %systemdrive% >nul
exit /b

Windows 2000 / Windows PE

@echo off

call :isAdmin
if %errorlevel% == 0 (
echo Running with admin rights.
) else (
echo Error: Access denied.
)

pause >nul
exit /b

:isAdmin
sfc 2>&1 | find /i "/SCANNOW" >nul
exit /b

适用于

Windows 2000 Windows XP Windows Vista Windows 7 Windows 8 Windows 8.1 --- Windows体育