'****************************************************
'		Script Witten by Larry Heintz
'		April 2006 www.windowsadminscripts.com
' This script will enumerate all groups on the local
' computer or remote computer.
'
' Script Usage: cscript groups.vbs /computer:[ComputerName]
'****************************************************
Dim args,computername
Set args = Wscript.Arguments.Named
computername = args.Item("computer")

if wscript.arguments.count = 0 then
	wscript.echo "Script Usage: cscript groups.vbs /computer:[ComputerName]"
elseif args.exists("computer") then
	if computername = "" then
		computername = getComputer()
	else
		computername = computername
	end if
	Call enumGroups(computername)
else
	wscript.echo "Script Usage: cscript groups.vbs /computer:[ComputerName]"
end if

Function enumGroups(computername)
On Error Resume Next
Dim objgroup,egroup,count
count = 0
Set objgroup = GetObject("WinNT://" & computername)
if not (errorChecking (computername)) then
	wscript.echo "Retrieving groups..."
	objgroup.filter = Array("group")
		For Each egroup in objgroup
			count = count + 1
			wscript.echo egroup.name
		next
	wscript.echo ""
	wscript.echo "There are " & count & " Groups on " & ucase(computername)
end if
Set objgroup = nothing
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
