'****************************************************
'		Script Witten by Larry Heintz
'		March 2006 www.windowsadminscripts.com
' This script will enumerate user and groups on a
' server. There a few different usages for the script.
' You can enumerate all users or all groups. You can
' also enumerate just the users in a certain group.
' All the information is written to a comma delimited
' text file, except the enumeration of groups.
'
' Script Usage:
' Enum Users: cscript users.vbs /computer:[ComputerName] /group:"[GroupName]"
' Enum Groups: cscript users.vbs /computer:[ComputerName] /group:list
' Enum All Users: cscript users.vbs /computer:[ComputerName] /group:all
'****************************************************
Dim args,computername,group,logPath
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
group = lcase(args.Item("group"))
logPath = getLogPath()

if wscript.arguments.count = 0 then
	wscript.echo "Script Usage:"
	wscript.echo "Enum Users: cscript users.vbs /computer:[ComputerName] /group:[GroupName]"
	wscript.echo "Enum Groups: cscript users.vbs /computer:[ComputerName] /group:list"
	wscript.echo "Enum All Users: cscript users.vbs /computer:[ComputerName] /group:all"
else
	if computername = "" then
		computername = getComputer()
	else
		computername = computername
	end if
	if group = "" then
		group = "users"
	elseif group = lcase("all") then
		Call enumUsers(computername)
		wscript.quit
	elseif group = lcase("list") then
		Call enumGroups(computername)
		wscript.quit
	else
		group = group
	end if
	Call enumUsersGroup(computername,group)
end if

Function enumUsers(computername)
On Error Resume Next
Dim objusers,euser,count
count = 0
wscript.echo "Starting script..."
Set objusers = GetObject("WinNT://" & computername)
if not (errorChecking (computername)) then
	objusers.filter = Array("user")
		For Each euser in objusers
			count = count + 1
			Call writetoLog(euser.name,"N/A",computername)
			wscript.echo euser.name
		next
end if
Set objgroup = nothing
wscript.echo ""
wscript.echo "There are " & count & " Users on " & ucase(computername)
wscript.echo "Ending script..."
End Function

Function enumUsersGroup(computername,group)
On Error Resume Next
Dim objgroup,member,count
count = 0
wscript.echo "Starting script..."
Set objgroup = GetObject("WinNT://" & computername & "/" & group & ",group")
if not (errorChecking (computername)) then
	for each member in objgroup.members
		count = count + 1
		Call writetoLog(member.name,group,computername)
		wscript.echo member.name & "," & group
	next
end if
Set objgroup = nothing
wscript.echo ""
wscript.echo "There are " & count & " Users in the " & ucase(group) & " group on " & ucase(computername)
wscript.echo "Ending script..."
End Function

Function enumGroups(computername)
On Error Resume Next
Dim objgroup,egroup,count
count = 0
wscript.echo "Starting script..."
Set objgroup = GetObject("WinNT://" & computername)
if not (errorChecking (computername)) then
	objgroup.filter = Array("group")
		For Each egroup in objgroup
			count = count + 1
			wscript.echo egroup.name
		next
end if
Set objgroup = nothing
wscript.echo ""
wscript.echo "There are " & count & " Groups on " & ucase(computername)
wscript.echo "Ending script..."
End Function

Function writetoLog(username,group,computername)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile(logPath & "\users_" & computername & ".txt", 8,True)
	objFSOwriteline.WriteLine(username & "," & group)
	objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
	temp2 = temp2 & temp(i) & "\"
next
getLogPath = temp2
End Function

Function getComputer()
	Dim objNet
	Set objNet = WScript.CreateObject("WScript.Network") 
	getComputer = objNet.ComputerName 
	Set objNet = Nothing 
End Function

Function errorChecking(ComputerName) 
errorChecking = False 
if err.number <> 0 then 
	wscript.echo "Unable to connect to " & ucase(ComputerName)
	err.Clear () 
	errorChecking = True
end if 
end Function
