Results 1 to 2 of 2

Thread: script to add multple users to local admin group on servers

  1. #1
    tdubb Guest

    script to add multple users to local admin group on servers

    Can someone please correct me if I am doing anything wrong.

    The script should read 2 files

    users.txt - list of domain users
    hosts.txt - list of hosts

    It will read the users.txt and add the users as local admins to all the
    hosts in host.txt

    ----------------------------------------------------------------------------------------


    ' Script to add domain user to local administrators group

    Option Explicit

    Dim objNetwork, strDomain, strComputer, strFilename
    Dim strUser, objUser, objGroup, objFSO, objTextStream
    Dim StrUserFilename


    Const FOR_READING = 1


    ' Open text file of computer names.

    strFilename = "c:\scripts\hosts.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextStream = objFSO.OpenTextFile(strFilename, FOR_READING)
    set objNetwork = CreateObject("Wscript.Network")
    strDomain = objNetwork.UserDomain


    ' Open text file of user names

    strUserFilename = "c:\scripts\users.txt
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextStream = objFSO.OpenTextFile(strFilename, FOR_READING)
    set objNetwork = CreateObject("Wscript.Network")
    strDomain = objNetwork.UserDomain
    'Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")



    ' Read text file.

    Do Until objTextStream.AtEndOfStream

    ' Trim leading and trailing blanks.

    strComputer = Trim(objTextStream.ReadLine)
    strUser = Trim(objTextStream.ReadLine)


    ' Skip blank lines.

    If (strComputer <> "") Then

    ' Bind to local Administrators group on remote computer.
    ' bind to domain user

    ' Trap error if computer does not exist or is off line.

    On Error Resume Next
    Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
    Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")

    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Fail to bind to group on " & strComputer
    Else
    On Error GoTo 0
    ' Check if user is already a member.
    If (objGroup.IsMember(objUser.AdsPath) = False) Then
    ' Add the domain user to the local group.
    objGroup.Add(objUser.ADsPath)
    End If
    End If
    End If
    Loop



  2. #2
    Richard Mueller [MVP] Guest

    Re: script to add multple users to local admin group on servers

    tdubb wrote:

    > Can someone please correct me if I am doing anything wrong.
    >
    > The script should read 2 files
    >
    > users.txt - list of domain users
    > hosts.txt - list of hosts
    >
    > It will read the users.txt and add the users as local admins to all the
    > hosts in host.txt
    >
    > ----------------------------------------------------------------------------------------
    >
    >
    > ' Script to add domain user to local administrators group
    >
    > Option Explicit
    >
    > Dim objNetwork, strDomain, strComputer, strFilename
    > Dim strUser, objUser, objGroup, objFSO, objTextStream
    > Dim StrUserFilename
    >
    >
    > Const FOR_READING = 1
    >
    >
    > ' Open text file of computer names.
    >
    > strFilename = "c:\scripts\hosts.txt"
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > Set objTextStream = objFSO.OpenTextFile(strFilename, FOR_READING)
    > set objNetwork = CreateObject("Wscript.Network")
    > strDomain = objNetwork.UserDomain
    >
    >
    > ' Open text file of user names
    >
    > strUserFilename = "c:\scripts\users.txt
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > Set objTextStream = objFSO.OpenTextFile(strFilename, FOR_READING)
    > set objNetwork = CreateObject("Wscript.Network")
    > strDomain = objNetwork.UserDomain
    > 'Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
    >
    >
    >
    > ' Read text file.
    >
    > Do Until objTextStream.AtEndOfStream
    >
    > ' Trim leading and trailing blanks.
    >
    > strComputer = Trim(objTextStream.ReadLine)
    > strUser = Trim(objTextStream.ReadLine)
    >
    >
    > ' Skip blank lines.
    >
    > If (strComputer <> "") Then
    >
    > ' Bind to local Administrators group on remote computer.
    > ' bind to domain user
    >
    > ' Trap error if computer does not exist or is off line.
    >
    > On Error Resume Next
    > Set objGroup = GetObject("WinNT://" & strComputer &
    > "/Administrators,group")
    > Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
    >
    > If (Err.Number <> 0) Then
    > On Error GoTo 0
    > Wscript.Echo "Fail to bind to group on " & strComputer
    > Else
    > On Error GoTo 0
    > ' Check if user is already a member.
    > If (objGroup.IsMember(objUser.AdsPath) = False) Then
    > ' Add the domain user to the local group.
    > objGroup.Add(objUser.ADsPath)
    > End If
    > End If
    > End If
    > Loop
    >
    >


    In your program the object reference objTextStream refers to two different
    files. The last "Set objTextStream" effectively overwrites the first. You
    need to use two different object variable names, perhaps objTextStream1 for
    computers and objTextStream2 for users. Then you need to nest the
    enumeration of the two files. In fact, if the outer loop enumerates
    computers and the inner loop enumerates users, you will need to close and
    reopen the file of users for each computer. Something similar to below (not
    tested). Watch out for line wrapping:
    =============
    Option Explicit

    Dim objNetwork, strDomain, strComputer, strFilename
    Dim strUser, objUser, objGroup, objFSO, objTextStream1
    Dim objTextStream2, StrUserFilename

    Const FOR_READING = 1

    ' Open text file of computer names.
    strFilename = "c:\scripts\hosts.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextStream1 = objFSO.OpenTextFile(strFilename, FOR_READING)
    set objNetwork = CreateObject("Wscript.Network")
    strDomain = objNetwork.UserDomain

    ' Specify text file of user names.
    strUserFilename = "c:\scripts\users.txt

    ' Read text file of computer NetBIOS names.
    Do Until objTextStream1.AtEndOfStream
    ' Trim leading and trailing blanks.
    strComputer = Trim(objTextStream1.ReadLine)
    ' Skip blank lines.
    If (strComputer <> "") Then
    ' Bind to the local Administrators group on the computer.
    ' Trap possible error.
    On Error Resume Next
    Set objGroup = GetObject("WinNT://" & strComputer &
    "/Administrators,group")
    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Fail to bind to group on " & strComputer
    Else
    On Error GoTo 0
    ' Open text file of user names.
    Set objTextStream2 = objFSO.OpenTextFile(strUserFilename,
    FOR_READING)
    ' Read the text file of user names.
    Do Until objTextStream2.AtEndOfStream
    ' Trim leading and trailing blanks.
    strUser = Trim(objTextStream2.ReadLine)
    ' Skip blank lines.
    If (strUser <> "") Then
    ' Bind to the domain user object.
    ' Trap possible error.
    On Error Resume Next
    Set objUser = GetObject("WinNT://" & strDomain & "/" &
    strUser & ",user")
    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Fail to bind to user " & strUser
    Else
    On Error GoTo 0
    ' Check if user is already a member.
    If (objGroup.IsMember(objUser.AdsPath) = False) Then
    ' Add the domain user to the local group.
    objGroup.Add(objUser.ADsPath)
    End If
    End If
    End If
    Loop
    ' Close the text file of user names.
    objTextStream2.Close
    End If
    End If
    Loop

    ' Close text file of computer NetBIOS names.
    objTextStream1.Close

    --
    Richard Mueller
    Microsoft MVP Scripting and ADSI
    Hilltop Lab - http://www.rlmueller.net
    --



Similar Threads

  1. adding domain users automatically to the local admin group
    By The Shadow in forum Active Directory
    Replies: 3
    Last Post: 07-06-2011, 10:57 PM
  2. Add domain user\group to local admin group problem
    By Landon in forum Active Directory
    Replies: 3
    Last Post: 16-10-2009, 09:30 PM
  3. Granting Domain Users Local Admin Rights
    By Jasonholt in forum Windows Security
    Replies: 2
    Last Post: 22-04-2009, 10:29 PM
  4. can't add users to the local admin group
    By ride1600@cox.net in forum Windows Server Help
    Replies: 5
    Last Post: 07-07-2008, 06:37 PM
  5. Add users to local admin via login script
    By Allanoo in forum Active Directory
    Replies: 3
    Last Post: 26-04-2006, 09:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,114,462.81298 seconds with 17 queries