Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to check if a user is an administrator on the local machine

General

Function to check if a user is an administrator on the local machine

by  djhawthorn  Posted    (Edited  )
Two versions; one reliant on WMI and the other on registry write/cleanup

Code:
'************************************
'*
'* User Is Administrator (Function)
'* Version 4.0
'*
'************************************
'This function checks to see if the currently logged-in user is an administrator
'Note: Requires WMI
Function UserIsAdministrator
  'The local machine
  strComputer = "."

  'Connect to the registry using WMI
  Set fncObjReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

  'The key path to check access to
  fncStrKeyPath = "SYSTEM\CurrentControlSet"

  'Check the users access to delete this component of the registry, and return the result
  fncObjReg.CheckAccess HKEY_LOCAL_MACHINE, fncStrKeyPath, DELETE, UserIsAdministrator
End Function




Set WshShell = CreateObject("Wscript.Shell")

'************************************
'*
'* User Is Administrator (Function)
'* Version 3.01
'*
'************************************
'This function checks to see if the currently logged-in user is an administrator
Function UserIsAdministrator
  'If we cant create the registry entry when we try, it will return an error, so we need to resume
  On Error Resume Next

  'Try to write to the root of the registry (only Administrator's can do this)
  WshShell.RegWrite "HKLM\TestAdminAccess", "Access Granted"

  'Catch the error
  If (Err.Number <> 0) Then
    'If it errors, we can't create the reg key
    UserIsAdministrator = false
  Else
    'Otherwise, we are an administrator; remove the key
    WshShell.RegDelete "HKLM\TestAdminAccess"

    'Set the flag to say the user has access
    UserIsAdministrator = true
  End If

  'Clear any errors
  Err.Clear

  'Turn error reporting back on
  On Error Goto 0
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top