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

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

    User may be not in local administrator group. For example - domain admins. UAC usually blocks admin access to registry, shares e.t.c. even for administrators(onl y manual "run as admin" gets right)...

    Here is my crazy way:

    Set Shell = CreateObject("WScript.Shell")
    set fso = CreateObject("Scripting.FileSystemObject")
    strCheckFolder = Shell.ExpandEnvironmentStrings("%USERPROFILE%") 
    strCheckFolder = strCheckFolder+"\TempFolder"
    
    if fso.FolderExists(strCheckFolder) then
            fso.DeleteFolder(strCheckFolder)
    end if
    
    fso.CreateFolder(strCheckFolder)
    tempstr = "cmd.exe /u /c chcp 65001 | whoami /all >" & strCheckFolder & "\rights.txt"
    Shell.run tempstr
    
    tempstr = strCheckFolder & "\rights.txt"
    WScript.Sleep 200
    Set txtFile = FSO.OpenTextFile(tempstr,1)
    
    IsAdmin = False
    
    Do While Not txtFile.AtEndOfStream
      x=txtFile.Readline
      If InStr(x, "S-1-5-32-544") Then
          IsAdmin = True
      End If
    Loop
    
    txtFile.Close
    

提交回复
热议问题