了解用户是否具有VBScript的管理权限的最佳方法

了解用户是否具有VBScript的管理权限的最佳方法,vbscript,privileges,Vbscript,Privileges,我需要检查执行脚本的用户是否具有计算机上的管理权限 我指定了执行脚本的用户,因为该脚本可能是使用类似于“Runas”的东西与登录用户以外的用户一起执行的 @哈维尔:这两种解决方案都适用于安装了英文版Windows的电脑,但如果安装的是不同语言的电脑,则不适用。这是因为Administrators组不存在,名称不同,例如西班牙语。我需要在所有配置下都能工作的解决方案 如果要查看登录用户是否是管理员,可以使用脚本 Set objNetwork = CreateObject("Wscript.Netw

我需要检查执行脚本的用户是否具有计算机上的管理权限

我指定了执行脚本的用户,因为该脚本可能是使用类似于“Runas”的东西与登录用户以外的用户一起执行的


@哈维尔:这两种解决方案都适用于安装了英文版Windows的电脑,但如果安装的是不同语言的电脑,则不适用。这是因为Administrators组不存在,名称不同,例如西班牙语。我需要在所有配置下都能工作的解决方案

如果要查看登录用户是否是管理员,可以使用脚本

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
strUser = objNetwork.UserName

isAdministrator = false

Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
For Each objUser in objGroup.Members
    If objUser.Name = strUser Then
        isAdministrator = true        
    End If
Next

If isAdministrator Then
    Wscript.Echo strUser & " is a local administrator."
Else
    Wscript.Echo strUser & " is not a local administrator."
End If
当脚本使用“Runas”运行时,恐怕我不知道如何处理它。

有一段关于如何枚举组成员的代码(为了方便起见复制到这里,并编辑为不使用电子邮件地址):

然后,您可以编写一个函数来查看用户是否在列表中

Function IsAdmin(user)
    IsAdmin = InStr(RetrieveUsers("MachineName", "Administrators"), user) > 0
End Function
…并这样称呼它:

If IsAdmin("LocalAccount") Then
    Wscript.Echo "LocalAccount is an admin"
Else
    Wscript.Echo "LocalAccount is not an admin"
End If

通过这样做,您可以打破用户拥有脚本所需权限但不属于管理员的场景。与其检查组成员资格,不如检查您需要的特定功能。

我在公司网络上的Windows 7设备上试用了Tim C的解决方案,我确实拥有管理员权限。但它显示我的用户没有管理员权限

取而代之的是,我使用了一种黑客方法,因为在cmd提示符中调用“defrag”需要管理员访问权限。当它工作时,请注意XP和7(可能是Windows的未来版本)在返回代码上的不同。可能会有比碎片整理更一致的选择,但它现在起作用

Function isAdmin
    Dim shell
    set shell = CreateObject("WScript.Shell")
    isAdmin = false
    errlvl = shell.Run("%comspec% /c defrag /?>nul 2>nul", 0, True)
    if errlvl = 0 OR errlvl = 2 Then '0 on Win 7, 2 on XP
        isAdmin = true
    End If
End Function
检查“\\computername\Admin$\system32”怎么样


又是一种快速而肮脏的方法。如果IsNotAdmin,则返回0

Function IsNotAdmin()
    With CreateObject("Wscript.Shell")
        IsNotAdmin = .Run("%comspec% /c OPENFILES > nul", 0, True)
    End With
End Function
使用“localhost”而不是真正的主机名会使脚本运行时间增加约10倍
我的最终代码是:

' get_admin_status.vbs
Option Explicit

Dim oGroup:   Set oGroup   = GetObject("WinNT://localhost/Administrators,group")
Dim oNetwork: Set oNetwork = CreateObject("Wscript.Network")

Dim sSearchPattern: sSearchPattern = "WinNT://" & oNetwork.UserDomain & "/" & oNetwork.UserName

Dim sMember
For Each sMember In oGroup.Members
  If sMember.adsPath = sSearchPattern Then
    ' Found...
    Call WScript.Quit(0)
  End If
Next

' Not found...
Call WScript.Quit(1)
如果当前用户是本地管理员,则此脚本返回退出代码0。

用法:cscript.exe get_admin_status.vbs

我知道此线程非常旧,并标记为已回答,但答案并没有给出OP询问的内容

对于搜索和查找此页面的任何其他人,这里有一个替代方案,它基于权限而不是组成员身份进行报告,以便Runas Administrator将管理员权限显示为True

Option Explicit 

msgbox isAdmin(), vbOkonly, "Am I an admin?"

Private Function IsAdmin()
    On Error Resume Next
    CreateObject("WScript.Shell").RegRead("HKEY_USERS\S-1-5-19\Environment\TEMP")
    if Err.number = 0 Then 
        IsAdmin = True
    else
        IsAdmin = False
    end if
    Err.Clear
    On Error goto 0
End Function

用户可能不在本地管理员组中。例如,域管理员。 UAC通常会阻止管理员访问注册表,甚至为管理员共享e.t.c.(只有手动“以管理员身份运行”才正确)

这是我疯狂的方式:

Set Shell = CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
strCheckFolder = Shell.ExpandEnvironmentStrings("%USERPROFILE%") 
strCheckFolder = strCheckFolder+"\TempFolder"

if fso.FolderExists(strCheckFolder) then
        fso.DeleteFolder(strCheckFolder)
end if

fso.CreateFolder(strCheckFolder)
tempstr = "cmd.exe /u /c chcp 65001 | whoami /all >" & strCheckFolder & "\rights.txt"
Shell.run tempstr

tempstr = strCheckFolder & "\rights.txt"
WScript.Sleep 200
Set txtFile = FSO.OpenTextFile(tempstr,1)

IsAdmin = False

Do While Not txtFile.AtEndOfStream
  x=txtFile.Readline
  If InStr(x, "S-1-5-32-544") Then
      IsAdmin = True
  End If
Loop

txtFile.Close

嗨,蒂姆C,谢谢。我检查了一下,我的情况似乎也不错。我得到的用户名不是记录的用户名,而是脚本正在执行的用户名。只有一条评论。有点慢。我在启动HTML页面时调用此脚本,大约需要2/3秒。如果用户不是直接在Administrators组中,而是通过一些组成员身份,则此脚本不起作用。我同意这是一种更好的实现方法,但这是一种要求,即用户具有安装软件的管理权限,因此在我看来检查会更容易。当我已经以管理员身份运行时,这会提示我UAC程序提升,如果我选择是,则返回0。在我的普通帐户下运行时,它返回1,但没有UAC提示。我不喜欢这种方法。这可以检测到我何时以“管理员”身份打开了MS Access。在Windows 10 64位和Office 2016 32位上测试。
' get_admin_status.vbs
Option Explicit

Dim oGroup:   Set oGroup   = GetObject("WinNT://localhost/Administrators,group")
Dim oNetwork: Set oNetwork = CreateObject("Wscript.Network")

Dim sSearchPattern: sSearchPattern = "WinNT://" & oNetwork.UserDomain & "/" & oNetwork.UserName

Dim sMember
For Each sMember In oGroup.Members
  If sMember.adsPath = sSearchPattern Then
    ' Found...
    Call WScript.Quit(0)
  End If
Next

' Not found...
Call WScript.Quit(1)
Option Explicit 

msgbox isAdmin(), vbOkonly, "Am I an admin?"

Private Function IsAdmin()
    On Error Resume Next
    CreateObject("WScript.Shell").RegRead("HKEY_USERS\S-1-5-19\Environment\TEMP")
    if Err.number = 0 Then 
        IsAdmin = True
    else
        IsAdmin = False
    end if
    Err.Clear
    On Error goto 0
End Function
Set Shell = CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
strCheckFolder = Shell.ExpandEnvironmentStrings("%USERPROFILE%") 
strCheckFolder = strCheckFolder+"\TempFolder"

if fso.FolderExists(strCheckFolder) then
        fso.DeleteFolder(strCheckFolder)
end if

fso.CreateFolder(strCheckFolder)
tempstr = "cmd.exe /u /c chcp 65001 | whoami /all >" & strCheckFolder & "\rights.txt"
Shell.run tempstr

tempstr = strCheckFolder & "\rights.txt"
WScript.Sleep 200
Set txtFile = FSO.OpenTextFile(tempstr,1)

IsAdmin = False

Do While Not txtFile.AtEndOfStream
  x=txtFile.Readline
  If InStr(x, "S-1-5-32-544") Then
      IsAdmin = True
  End If
Loop

txtFile.Close