?
Solved

Remove all users from Local Administrator Group and add the user in the file.

Posted on 2008-11-12
12
Medium Priority
?
339 Views
Last Modified: 2012-05-05
Hi,

Remove all users from Local Administrator Group and add the user in the file.
I have a txt file that has

machine name,NTlogin

When run script has to remove any user in the administrator fgroup and add just the users in the txt file next to the machine name.

Need a results csv or txt that shows what was removed and what was added.

Regards
Sharath
0
Comment
Question by:bsharath
  • 6
  • 6
12 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22941313
That a local or domain login?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22941331
Try this...
'Read machine name,NTlogin from log file:
 
strLogFile = "C:\Logfile.txt"
strOutput = "C:\Results.txt"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile(strLogFile)
Dim objOut : Set objOut = objFSO.CreateTextFile(strOutput)
 
Do While Not objFile.AtEndOfStream
  arrData = Split(objFile.ReadLine, ",")
  strPC = Trim(arrData(0))
  strUser = Trim(arrData(1))
 
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  Dim objUser : Set objUser = GetObject("WinNT://" & strPC & "/" & strUser)
  objAdmin.Add(objUser.AdsPath)
 
  For Each objUser In objAdmin.Members
    If objUser.Name <> strUser Then objAdmin.Remove(objUser.AdsPath)
  Next
  Set objUser = Nothing
Loop

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 22941333
That is a Domain Login in the txt files
The group is local Administrator group of each machine
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 67

Expert Comment

by:sirbounty
ID: 22941360
Forget that first one then...
Second question - is it going to be 'just' the user's name, or the full path to it (cn=username,dc=domain,dc=com)?
0
 
LVL 11

Author Comment

by:bsharath
ID: 22941377
Just the user name in the txt file
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22941617

'Read machine name,NTlogin from log file:
Const ADS_SCOPE_SUBTREE = 2
 
strLogFile = "C:\Logfile.txt"
strOutput = "C:\Results.txt"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile(strLogFile)
Dim objOut : Set objOut = objFSO.CreateTextFile(strOutput)
 
Do While Not objFile.AtEndOfStream
  arrData = Split(objFile.ReadLine, ",")
  strPC = Trim(arrData(0))
  strUser = FindUser(Trim(arrData(1)))
  Set objNewUser = GetObject(strUser)
 
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  objAdmin.Add(objNewUser.AdsPath)
 
  For Each objUser In objAdmin.Members
    If objUser.Name <> objNewUser.CN Then objAdmin.Remove(objUser.AdsPath)
  Next
  Set objUser = Nothing
Loop
 
wscript.quit
 
Function FindUser (UserName)
  Dim rootDSE : Set rootDSE = GetObject("LDAP://rootDSE")
  strADsPath = rootDSE.Get("defaultNamingContext")
  Dim objConn : Set objConn = CreateObject("ADODB.Connection")
  Dim objCmd : Set objCmd = CreateObject("ADODB.Command")
  objConn.Provider = "ADsDSOObject"
  objConn.Open "Active Directory Provider"
  Set objCmd.ActiveConnection = objConn
  objCmd.CommandText = "Select ADsPath From 'LDAP://" & strADsPath & "' Where objectClass='user' And Name='" & UserName & "'"
  objCmd.Properties("Page Size") = 1000
  objCmd.Properties("Timeout") = 300
  objCmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
  Dim objRS : Set objRS = objCmd.Execute
 
  objRS.MoveFirst
  If Not objRS.EOF Then FindUser=objRS.Fields(0).Value
End Function

Open in new window

0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 22941684
Oops - forgot the logging...
'Read machine name,NTlogin from log file:
Const ADS_SCOPE_SUBTREE = 2
 
strLogFile = "C:\Logfile.txt"
strOutput = "C:\Results.txt"
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile(strLogFile)
Dim objOut : Set objOut = objFSO.CreateTextFile(strOutput)
 
Do While Not objFile.AtEndOfStream
  arrData = Split(objFile.ReadLine, ",")
  strPC = Trim(arrData(0))
  strUser = FindUser(Trim(arrData(1)))
  Set objNewUser = GetObject(strUser)
 
  Dim objAdmin : Set objAdmin = GetObject("WinNT://" & strPC & "/Administrators")
  On Error Resume Next
  objAdmin.Add(objNewUser.AdsPath)
  If Err.Number <> = Then
    objOut.WriteLine "Couldn't add " & strUser & " to " & strPC
  Else
    objOut.WriteLine "Added " & strUser & " to " & strPC    
  End If
  On Error Goto 0
 
  For Each objUser In objAdmin.Members
    If objUser.Name <> objNewUser.CN Then objAdmin.Remove(objUser.AdsPath)
    objOut.WriteLine "Removed " & objUser.Name & " from " & strPC    
  Next
  Set objUser = Nothing
Loop
 
objOut.Close
wscript.quit
 
Function FindUser (UserName)
  Dim rootDSE : Set rootDSE = GetObject("LDAP://rootDSE")
  strADsPath = rootDSE.Get("defaultNamingContext")
  Dim objConn : Set objConn = CreateObject("ADODB.Connection")
  Dim objCmd : Set objCmd = CreateObject("ADODB.Command")
  objConn.Provider = "ADsDSOObject"
  objConn.Open "Active Directory Provider"
  Set objCmd.ActiveConnection = objConn
  objCmd.CommandText = "Select ADsPath From 'LDAP://" & strADsPath & "' Where objectClass='user' And Name='" & UserName & "'"
  objCmd.Properties("Page Size") = 1000
  objCmd.Properties("Timeout") = 300
  objCmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
  Dim objRS : Set objRS = objCmd.Execute
 
  objRS.MoveFirst
  If Not objRS.EOF Then FindUser=objRS.Fields(0).Value
End Function

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 22941697
Thank U Shall try and get back
0
 
LVL 11

Author Comment

by:bsharath
ID: 22958075
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Remove all and add this user.vbs
Line:      20
Char:      20
Error:      Syntax error
Code:      800A03EA
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22958988
If Err.Number <> = Then
should read:
If Err.Number <> 0 Then
0
 
LVL 11

Author Comment

by:bsharath
ID: 22966210
I get this

---------------------------
Windows Script Host
---------------------------
Script:      C:\Remove all and add this user.vbs
Line:      51
Char:      3
Error:      Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.
Code:      800A0BCD
Source:       ADODB.Recordset

---------------------------
OK  
---------------------------
0
 
LVL 11

Author Comment

by:bsharath
ID: 22985116
Sirbounty any view...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question