Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



script to add multple users to local admin group on servers

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 21-03-2008
tdubb
 
Posts: n/a
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



Reply With Quote
  #2  
Old 21-03-2008
Richard Mueller [MVP]
 
Posts: n/a
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
--


Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 12:20 PM.