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

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

提交回复
热议问题