[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 934
  • Last Modified:

VB Script determine if user is domain admin

How can I get a VB Script to check whether the user that is running it has Domain Admin rights?

If the user isn't a Domain Admin I want the script to display an error and quit.
1 Solution
Chris DentPowerShell DeveloperCommented:

This should do, it's a bit long, but it chases down nested group memberships:

Function RecursiveIsMember(strGroup, arrGroups, x)
      ' Return Type: Boolean
      ' Goes through Nested Groups until either booIsMember is True or there are no more groups to check
      Dim objADSystemInfo, objUser, objGroup
      Dim strGroupDN
      Dim arrTemp
      Dim booIsMember

      booIsMember = False

      On Error Resume Next
      If Not IsArray(arrGroups) Then
            Set objADSystemInfo = CreateObject("ADSystemInfo")
            Set objUser = GetObject("LDAP://" & objADSystemInfo.UserName)
            arrGroups = objUser.GetEx("memberOf")
            Set objUser = Nothing
            Set objADSystemInfo = Nothing
      End If

      For Each strGroupDN in arrGroups
            Set objGroup = GetObject("LDAP://" & strGroupDN)
            ' WScript.Echo Space(x) & objGroup.Get("name")
            If Err.Number = 0 Then
                  If LCase(objGroup.Get("name")) = LCase(strGroup) Then
                        booIsMember = True
                        Exit For
                        arrTemp = objGroup.GetEx("memberOf")
                        If Err.Number = 0 Then
                              y = x + 2
                              booIsMember = IsMember(strGroup, arrTemp, y)
                              If booIsMember = True Then
                                    Exit For
                              End If
                        End If
                  End If
            End If
            Set objGroup = Nothing
      On Error Goto 0
      IsMember = booIsMember
End Function

If RecursiveIsMember("Domain Admins", "", 0) = False Then
      WScript.Echo "Error: Script must be executed by Domain Admin"
End If

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now