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
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
--