Submitted By: Ron Rosenkoetter

Adds a domain group or user to the local Administrators group.

Visual Basic
Edit|Remove
'Example
  ComputerName = "EXCHANGE01"
  Domain = "FABRIKAM"
  GroupName = "ExchangeAdmins"

'Add the group to the local administrators group
  Wscript.echo vbCrLf & "Adding " & Domain & "\" & GroupName & _
    " to the local administrators group on " & ComputerName
  Select Case AddGroupToLocalAdmins(ComputerName,Domain,GroupName)
    Case 0
      wscript.echo "  Succes"
    Case 1
      wscript.echo "  Failed - unable to add the account to the local administrators group"
    Case 2
      wscript.echo "  Already a member"
    Case 3
      wscript.echo "  Failed - Unable to connect to the computer"
  End Select
      
      


'#############################
'Function AddGroupToLocalAdmins

'Variables
  'ComputerName
  'Domain of Group being added
  'GroupName (User account can be used as well)
  
'Returns
  '0 if Group is added successfully to the local Admin group
  '1 if function was unable to add the Group to the local Admin group
  '2 if Group is already a member of the local Admin group
  '3 if the function was unable to connect to the specified Computer

  Function AddGroupToLocalAdmins(ComputerName,Domain,GroupName)

    On Error Resume Next

'Create an group object referencing the local Administrators group on the
'Computer
    Set objLocalGroup = GetObject("WinNT://" & ComputerName & "/Administrators, group")
    If Err.Number = 0 Then

'Check to see if the Group already exists in the local Admin group
      For each Group in objLocalGroup.Members 
        If Instr(ucase(Group.ADSPath),ucase(Domain & "/" & GroupName)) <> 0 Then
          AlreadyExists = True
        End If
      Next

'Add the specified group to the local Administrators group if it doesn't already
'exist
      If AlreadyExists = False Then
        objLocalGroup.Add("WinNT://" & Domain & "/" & GroupName)

        If Err.Number = 0 Then
          AddGroupToLocalAdmins = 0
        Else
          AddGroupToLocalAdmins = 1
          Err.Clear
        End If

      Else
        AddGroupToLocalAdmins = 2
      End If

    Else
      AddGroupToLocalAdmins =3
      Err.Clear
    End If
 
  End Function
      
'#############################