Script to check each machines Administrator group and get the member names into a file.

hi,

Script to check each machines Administrator group and get the member names into a file.
I need help with a script when run scan's every computer in the Domain or a txt file with machine names and get every member name thats within the local administrator groups
Into 1 csv/Excel file

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sr75Commented:
I did that for my work here.   You will just need to change the strPath and ensure that your users have access to that path to WRITE into.


'########################################################################################
'#											#
'#	Name:		LocAdmin.vbs							#
'#	Version:	1.0.0								#
'#	Created:	June 26th, 2009							#
'#	Modified:	n/a								#
'#	Author:		Martin Roeske							#
'#											#
'#	Description:	This script will log if a user is a member of the local admin	#
'#			group on the computer they log on to.				# 
'#											#
'#	Notes:		When ran as a logon script, it will query the local admin group	#
'#			and see if the logged on user is a local admin.  It will then 	#
'#			log the Username and the Computername that the user is a Local 	#
'#			Admin of to a log file.  The log file gets checked to see if	#
'#			username and computername combo already exist in the log.  If 	#
'#			it doesn't, then it will be logged.				#
'#											#
'########################################################################################
DIM objLocalGroup

Set objLocalGroup = GetObject("WinNT://./Administrators,group")
ProcessGroup(objLocalGroup)

set objLocalGroup = Nothing

Sub ProcessGroup(objGroup)
	
	DIM Net
	DIM strUser
	DIM strCName
	DIM objMember	

	set Net = CreateObject("Wscript.network")
	strUser = Net.Username
	strCName = Net.ComputerName
	
	If LCase(strCName) = "lpi-esmith" Then 
		Wscript.quit
	End If

	For Each objMember In objGroup.Members

		If (LCase(objMember.Class) = "group") Then
			Call ProcessGroup(objMember)
		End If

		If objMember.Name = strUser then
			func_LogIt strUser, strCName
		End If
	Next

	set Net = Nothing
	set strUser = Nothing
	set strCName = Nothing
	set objMemeber = Nothing

End Sub

Function func_LogIt(strU, strC)

	DIM strPath
	DIM strFile
	DIM FSO
	DIM rFile
	DIM rLog
	DIM tLog
	DIM wFile

	strPath = "\\server\share$"
	strFile = strPath & "\LocalAdmin.Log"

	set FSO = CreateObject("Scripting.FileSystemObject")
		
	If FSO.FileExists(strFile) then
		Set rFile = FSO.OpenTextFile(strFile)
		rLog = ""
		Do Until rFile.AtEndOfStream
			rLine = rFile.ReadLine
			If Not inStr(rLine, strU) and Not inStr(rLine, strC) then
				rLog = rLog & rLine & vbcrlf
				tLog = strU & vbtab & vbtab & strC & vbcrlf
			Else
				rLog = rLog & rLine & vbcrlf
			End If
			rLine = ""
		Loop
		rFile.Close
		rLog = rLog & tLog
	Else
		rLog = strU & vbtab & vbtab & strC & vbcrlf
	End If 

	
	set wFile = FSO.OpenTextFile(strFile, 2, True)
	wFile.write rLog
	wFile.Close

	set strPath = Nothing
	set strFile = Nothing
	set FSO = Nothing
	set rFile = Nothing
	set rLog = Nothing
	set tLog = Nothing
	set wFile = Nothing

End Function

Open in new window

0
bsharathAuthor Commented:
Thanks i want a way to run them on all machines on the Domain or a list of machine names in a txt file.
I already have a logon script that can do this. but need a way to trigger it from one machine
0
merowingerCommented:
check out this!
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objPCs = objFSO.OpenTextFile("D:\Computers.txt")
Set objOutput = objFSO.CreateTextFile("D:\Output.csv")

Do While Not objPCs.AtEndOfStream
	strCurrentPc = objPCs.ReadLine
	On Error Resume Next
	Set objGroup = GetObject("WinNT://" & strCurrentPc & "/Administrators,group")
	If Err.Number = 0 Then
		objOutput.WriteLine "Groups for Computer:" &strCurrentPc
		Call EnumGroup(objGroup, "")
	Else
		objOutput.WriteLine "Computer N/A:" &strCurrentPc
	End If


Loop

Sub EnumGroup(objGroup, strOffset)
For Each objMember In objGroup.Members
	objOutput.WriteLine strOffset & objMember.Name & " (" & objMember.Class &")"
	If (objMember.Class = "Group") Then
		Call EnumGroup(objMember, strOffset & "--")
	End If
Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

bsharathAuthor Commented:
I get this
---------------------------
Windows Script Host
---------------------------
Script:      C:\Find-All-Local_Administrator members.vbs
Line:      3
Char:      1
Error:      Permission denied
Code:      800A0046
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------

And if a machine with permission i get the log created but a lot of blank rows in the csv is created
0
merowingerCommented:
which scsript did you use? Mine?
Don't you have permissons onto the D:\ partition? Does it exist?
0
bsharathAuthor Commented:
yes yours. What happens if i dont have permissions on one of the
Yes D drive exists
0
merowingerCommented:
you can also take another path for the file...does not matter
0
bsharathAuthor Commented:
The script stops with the machine i have logged into and run the script does not have permissions on the remote machine the loop breaks.
Can it be as record the failure into the log and continue to the next machines
And
The csv thats generated creates a lot of empty lines between each member thats retrieved
0
merowingerCommented:
Sorry could you please post where the script stops and which error you see
0
rejoinderCommented:
I have looked over the script and made a minor change to clear the error message so that the script moves onto the next computer.
When you run the script I am interested to know if you are getting lots of blank lines again.  If you are - perhaps those groups are from another trusted domain and the system cannot resolve the name?!?  Anyway, please keep us posted.
On Error Resume Next 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objPCs = objFSO.OpenTextFile("Computers.txt") 
Set objOutput = objFSO.CreateTextFile("Output.csv") 
 
Do While Not objPCs.AtEndOfStream 
	strCurrentPc = objPCs.ReadLine 
	Set objGroup = GetObject("WinNT://" & strCurrentPc & "/Administrators,group") 
	If Err.Number = 0 Then 
		objOutput.WriteLine strCurrentPc
		Call EnumGroup(objGroup, "") 
	Else 
		err.clear
		objOutput.WriteLine strCurrentPc & " unvavailable"
	End If
Loop 
 
Sub EnumGroup(objGroup, strOffset) 
	For Each objMember In objGroup.Members 
		objOutput.WriteLine strOffset & objMember.Name & " (" & objMember.Class & ")" 
		If (objMember.Class = "Group") Then 
			Call EnumGroup(objMember, strOffset & "--") 
		End If 
	Next 
End Sub

wscript.echo "Done"

Open in new window

0
bsharathAuthor Commented:
Hi rejoinder.
I get as this...
I dont get the error but the excel is populated as attached

Can i have Data as this
machinename                       Machinename
member1                               member1
member2                               Member2
and so on                              And So on

I will have 2000+ computers to scan
having all of them in a colum is difficult to filter

0
rejoinderCommented:
Do you care about group names or do you just want the users?
0
bsharathAuthor Commented:
I want the Group names also.
Forgot the attachment
Capture.JPG
0
rejoinderCommented:
How about the above script with this tweak?
On Error Resume Next 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objPCs = objFSO.OpenTextFile("Computers.txt") 
Set objOutput = objFSO.CreateTextFile("Output.csv") 
 
Do While Not objPCs.AtEndOfStream 
	strCurrentPc = objPCs.ReadLine 
	Set objGroup = GetObject("WinNT://" & strCurrentPc & "/Administrators,group") 
	If Err.Number = 0 Then 
		objOutput.WriteLine strCurrentPc
		Call EnumGroup(objGroup, 1) 
	Else 
		err.clear
		objOutput.WriteLine """" & strCurrentPc & " unvavailable" & """"
	End If
Loop 
 
Sub EnumGroup(objGroup, intOffset) 
	For Each objMember In objGroup.Members
		strOffset = ""
		for n = 1 to intOffset
			strOffset = strOffset & """"","
		next
		objOutput.WriteLine strOffset & """" & objMember.Name & " (" & objMember.Class & ")" & """"
		If (objMember.Class = "Group") Then 
			Call EnumGroup(objMember, intOffset+1) 
		End If 
	Next 
End Sub

wscript.echo "Done"

Open in new window

0
bsharathAuthor Commented:
Its perfect
I dont want members names within the group. That can be removed.
0
rejoinderCommented:
Ah - put a comment in line 27 so it looks like this...

'Call EnumGroup(objMember, intOffset+1)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.