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

后端 未结 10 1912
不知归路
不知归路 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:01

    Ive tried Tim C's solution on a Windows 7 box on my company network where I do actually have admin rights. But it shows my user as not having admin rights.

    Instead I used a hackier method, as calling "defrag" in the cmd prompt requires admin access. While it works, be wary that XP and 7 (and possibly future versions of Windows) differ in the return code. There may be more consistent choices than defrag, but it works for now.

    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
    
    0 讨论(0)
  • 2021-01-02 22:06

    Yet another quick n dirty method. Returns <> 0 If IsNotAdmin

    Function IsNotAdmin()
        With CreateObject("Wscript.Shell")
            IsNotAdmin = .Run("%comspec% /c OPENFILES > nul", 0, True)
        End With
    End Function
    
    0 讨论(0)
  • 2021-01-02 22:11

    What about checking for "\\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 讨论(0)
  • 2021-01-02 22:11

    I know this thread is very old and marked answered but the answer isn't really giving what the OP asked about.

    For anyone else searching and finding this page, here is an alternative that does report based on rights not group membership so Runas Administrator shows admin rights as 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
    
    0 讨论(0)
  • 2021-01-02 22:21

    Using "localhost" instead of the real hostname increases the script runtime about 10x!
    My final code is:

    ' 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)
    

    This script returns exit code 0 if the current user is a local admin.
    Usage: cscript.exe get_admin_status.vbs

    0 讨论(0)
  • 2021-01-02 22:23

    By doing this you break scenarios where the user has the required privs for your script but does not belong to Administrators. Instead of checking for group membership, check for the specific abilities you require.

    0 讨论(0)
提交回复
热议问题