Best way to know if a user has administrative privileges from a VBScript

后端 未结 10 1979
不知归路
不知归路 2021-01-02 21:50

I need to check whether the user executing the script has administrative privileges on the machine.

I have specified the user executing the script because the script

10条回答
  •  滥情空心
    2021-01-02 22:25

    This article has a nice chunk of code on how to enumerate the members of a group (copied here for convenience and edited to not use email address):

    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
    

    You can then write a function to see if a user is in the list...

    Function IsAdmin(user)
        IsAdmin = InStr(RetrieveUsers("MachineName", "Administrators"), user) > 0
    End Function
    

    ...and call it like this:

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

提交回复
热议问题