Question : Query a txt file and add a user account into administrator group of each machine.

Hi,

Query a txt file and add a user account into administrator group of each machine.
I have the txt file as
Machinename;Userlogin

When script run check each machine and add that user into local admin. I want to add same login name thats next to the machine name but 2 similar logins from 2 Domains

Domain1\Userlogin1
Domain2\Userlogin1

Regards
sharath

Answer : Query a txt file and add a user account into administrator group of each machine.

try this out:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
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
Random Solutions  
 
programming4us programming4us