Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objLogFile, objInputFile, objFileSystem, objRootDSE, strDNSDomain, strQuery, adoRecordset, strName
Dim objNetwork, strComputer, strUser, objAdmin, strDomain
Dim lines, tokens, line
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objInputFile = objFileSystem.OpenTextFile("C:\temp\users.txt", 1)
lines = Split(objInputFile.ReadAll, vbNewline)
objInputFile.Close
Set objLogFile = objFileSystem.CreateTextFile("C:\temp\users.log", 2)
For each line In lines
tokens = Split(line, ";")
strComputer = Trim(tokens(0))
strUser = Trim(tokens(1))
if Ping(strComputer) = True then
objLogFile.WriteLine strComputer & " contacted"
AddUserGroup strComputer, strUser
Else
objLogFile.WriteLine strComputer & " could not be contacted"
end if
Next
objLogFile.Close
Sub AddUserGroup (strComputer, strUser)
' Retrieve local computer name and NetBIOS name of domain.
Set objNetwork = CreateObject("Wscript.Network")
strDomain = objNetwork.UserDomain
WScript.Echo "Add " & strUser & " to " & strComputer
' Bind to local Administrators group.
Set objAdmin = GetObject("WinNT://" & strComputer & "/Administrators,group")
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user with "pre-Windows 2000 logon" name.
strFilter = "(sAMAccountName=" & strUser & ")"
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values.
strName = adoRecordset.Fields("sAMAccountName").Value
' Check if user already a member of the local Administrators group.
If (objAdmin.IsMember("WinNT://" & strDomain & "/" & strName) = False) Then
On Error Resume Next
' Add this user to the group.
objAdmin.Add("WinNT://" & strDomain & "/" & strName)
If (Err.Number <> 0) Then
objLogFile.WriteLine strComputer & ";" & strUser & ";Failure"
else
WScript.Echo strName & " was added."
objLogFile.WriteLine strComputer & ";" & strUser & ";Success"
End If
else
WScript.Echo strName & " already exists."
objLogFile.WriteLine strComputer & ";" & strUser & ";Success"
End If
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
End Sub
Function Ping(strHost)
dim objPing, objRetStatus
set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & strHost & "'")
for each objRetStatus in objPing
if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
Ping = False
'WScript.Echo "Status code is " & objRetStatus.StatusCode
else
Ping = True
'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
end if
next
End Function
|