'*************************************************************
'		Script Witten by Larry Heintz       	    
'		April 2006 www.windowsadminscripts.com
' This script will create a new group. It will also check to 
' see if the group exists before trying to add it.
'
' Script Usage: creategroup.vbs /computer:[computername] /groupname:[groupname]
'*************************************************************
Dim args,computername,groupname
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
groupname = args.Item("groupname")

if computername = "" then
	computername = getComputer()
else
	computername = computername
end if

if wscript.arguments.count = 0 then
	wscript.echo "Script Usage: creategroup.vbs /computer:[computername] /groupname:[groupname]"
	wscript.quit
elseif args.exists("computer") and args.exists("groupname") then
	Call enumGroups()
else
	wscript.echo "Script Usage: creategroup.vbs /computer:[computername] /groupname:[groupname]"
	wscript.quit
end if

Function enumGroups()
if instr(groupArray(),lcase(groupname)) <> 0 then
	' Group Exists
	wscript.echo "The Group " & groupname & " already exists."
else
	' Group Doesnt Exists lets create it
	Call createGroup()
end if
End Function

Function createGroup()
Dim Container,Group
' Creates group if it does not already exist
Set Container = GetObject("WinNT://" & computername)
Set Group = Container.Create("Group", groupname)
	Group.SetInfo
Set Group = nothing
Set Container = nothing
wscript.echo "The Group " & ucase(groupname) & " has been created on " & ucase(computername) & "."
End Function

Function groupArray()
Dim Container,group
' Gets all groups on computer and add them to array
Set Container = GetObject("WinNT://" & computername)
	container.filter = Array("group")
	For Each group in Container
		groupArray = groupArray + lcase(group.name) & ","
	Next
Set Container = nothing
End Function	

Function getComputer()
	Dim objNet
	Set objNet = WScript.CreateObject("WScript.Network") 
	getComputer = objNet.ComputerName 
	Set objNet = Nothing 
End Function
