Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if global group already exists 1

Status
Not open for further replies.

rlee111

Technical User
Sep 10, 2001
27
GB
Hi I am using the code below to create new global security groups from names in a text file, if that file is updated with one or more names then the script run against it I get the error 'the object already exists' I can get round this using on error resume next, but would like to check for the existance of a global group within the script.

Can anyone help me out with the best way to do that?

'create new Global Groups from file c:\scripts\groups.txt

Const ForReading = 1
Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

'open text file for group names

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objGGFile = objFSO.OpenTextFile("c:\scripts\groups.txt", ForReading)

'Bind to users container in domain and create new Global Groups

Do Until objGGFile.AtEndOfStream

f_str = objGGFile.ReadLine
Err.Clear
Set objOU = GetObject("LDAP://cn=users,dc=domain,dc=domain,dc=domain,dc=biz")
If Err.Number <> 0 Then
Wscript.Echo Err.Number & "--" & Err.Description
Err.Clear
End If

Set objGroup = objOU.Create("Group", "cn=" & f_str)
objGroup.Put "sAMAccountName", f_str
objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or ADS_GROUP_TYPE_SECURITY_ENABLED
objGroup.Setinfo

Loop

objGGFile.Close


Thanks

Rob
 
Something like this?


Code:
'==========================================================================
'
' NAME: <BuildGroupsFromTxtFile.vbs>
'
' AUTHOR: Tek-Tips User rlee111
' Modifications: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 6/15/2005
'
' COMMENT: create new Global Groups from file c:\scripts\groups.txt
'
'==========================================================================

On Error Resume Next

Const ForReading = 1
Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000

'open text file for group names

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objGGFile = objFSO.OpenTextFile("c:\scripts\groups.txt", ForReading)

Do Until objGGFile.AtEndOfStream    
	groupName = objGGFile.ReadLine
	Err.Clear
	Set objGroup = GetObject("LDAP://cn=" & f_str & ",cn=users,dc=domain,dc=domain,dc=domain,dc=biz")
	If err Then
		DoIt = BuildGroup(groupName)
	Else
		msgbox "Group Already Exists"
	End if  
Loop

objGGFile.Close


Function BuildGroup(f_str) 
'Bind to users container in domain and create new Global Groups
    Set objOU = GetObject("LDAP://cn=users,dc=domain,dc=domain,dc=domain,dc=biz")      
    If Err.Number <> 0 Then
        Wscript.Echo Err.Number & "--" & Err.Description
        Err.Clear
    End If      
    
    Set objGroup = objOU.Create("Group", "cn=" & f_str)                        
    objGroup.Put "sAMAccountName", f_str
    objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or ADS_GROUP_TYPE_SECURITY_ENABLED       
    objGroup.Setinfo       
End Function

I hope you find this post helpful.

Regards,

Mark
 
Thanks Mark That done the job nicely

Regards

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top