知道用户是否具有来自VBScript的管理权限的最佳方法

问题描述:

我需要检查执行该脚本的用户是否在机器上具有管理权限。知道用户是否具有来自VBScript的管理权限的最佳方法

我已经指定用户执行脚本,因为脚本可能已经使用类似于“Runas”的类似登录以外的用户执行。

@Javier:这两种解决方案均可在装有英文版Windows的PC上运行,但如果安装的语言不同,则无法运行。这是因为管理员组不存在,名称与西班牙语不同。我需要解决方案在所有配置中工作。

,如果你想看看是否登录的用户可以使用脚本管理员

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 

我不知道如何处理它时,脚本与“运行方式”恐怕运行。

+0

嗨Tim C,谢谢。 我检查了它似乎工作正常也在我的情况。我得到的用户名不是记录的,而是脚本正在执行的那个。 只是一个评论。这有点慢。我从HTML页面启动时调用这个脚本,大约需要2/3秒。 – 2008-11-19 14:30:50

+2

如果用户不是直接在管理员组中,而是通过某些组成员资格,则这不起作用。 – Heinzi 2009-10-21 08:43:10

This article有代码如何枚举组的成员一个很好的块(复制在这里为了方便和编辑不使用电子邮件地址):

Function RetrieveUsers(domainName,grpName) 

dim GrpObj 
dim mbrlist 
dim mbr 

'------------------------------------------------------------------------------- 
' *** Enumerate Group Members *** 
'------------------------------------------------------------------------------- 

' Build the ADSI query and retrieve the group object 
Set GrpObj = GetObject("WinNT://" & domainName & "/" & grpName & ",group") 

' Loop through the group membership and build a string containing the names 
for each mbr in GrpObj.Members 
    mbrlist = mbrlist & vbTab & mbr.name & vbCrLf 
Next 

RetrieveUsers=mbrlist 

End Function 

然后,您可以编写一个函数来看看用户在列表...

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 

通过这样做,可以中断用户具有所需脚本权限但不属于管理员的情况。而不是检查组员资格,检查你需要的特定能力。

+0

我同意这将是一个更好的方式来实现它,但它是一个要求,用户有管理权限来安装软件,所以我认为检查会更容易。 – 2008-11-19 16:01:50

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

相反,我使用hackier方法,因为在cmd提示符下调用“碎片整理”需要管理员访问权限。尽管它有效,但请注意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”怎么办?

function IsLoggedInAsAdmin() 
    isAdmin = false 
    set shell = CreateObject("WScript.Shell") 
    computername = WshShell.ExpandEnvironmentStrings("%computername%") 
    strAdmin = "\\" & computername & "\Admin$\System32" 

    isAdmin = false 

    set fso = CreateObject("Scripting.FileSystemObject") 

    if fso.FolderExists(strAdmin) then 
     isAdmin = true 
    end if 

    IsLoggedInAsAdmin = isAdmin 
end function 

又一个快速的肮脏的方法。返回<> 0如果IsNotAdmin

Function IsNotAdmin() 
    With CreateObject("Wscript.Shell") 
     IsNotAdmin = .Run("%comspec% /c OPENFILES > nul", 0, True) 
    End With 
End Function 

Function isAdmin 
    Dim shell 
    Set shell = CreateObject("WScript.Shell") 
    isAdmin = false 
    errorLevel = shell.Run("%comspec% /c net session >nul 2>&1", 0, True) 
    if errorLevel = 0 
     isAdmin = true 
    End If 
End Function 

使用“本地主机”,而不是真正的主机名增加了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