We help IT Professionals succeed at work.
Get Started

VBScript Error: (Null): The Object Already Exists.

JB4375
JB4375 asked
on
821 Views
Last Modified: 2012-05-09
This script reads from an Excel spreadsheet to pull the group name, OU the group is to be created in, and the members that the group is to be populated with. Yesterday it was working like a charm: echoing that the group was created, and echoing as each member was added to the group.

The only thing I can figure, now that I'm writing this out, is that in my testing I created 3 groups in the 3 different OU's as opposed to 3 different groups in the same OU.

Now it creates the group, goes to the AddUsers function, locates the group, echos it's location, adds and echos the addition of the first member, and that's where the error occurs.

One other thing.... It never gets to the statement where it echos the value of introw.  

Thanks in advance!!
Dim strDescription, strGroupName,strGroupOU, strMembers, strUser, strUserID
Dim strDomain, strObjCategory, strObjType, strProvider, strTargetOU 
Dim objConnection, objCommand, objRecordSet 
Dim strCategory, strClass, strTarget, strFilter 
Dim objGroup, Group 
Dim introw


strMembers = ""
 
'Read from excel 
Set objExcelInput = CreateObject("Excel.Application") 
Set objWorkbook = objExcelInput.Workbooks.Open("c:\GroupListing.xls") 
introw = 2 'initialize to second row
 
Do Until objExcelInput.Cells(introw, 1).Value = "" 
	If objExcelInput.Cells(introw, 1).Value <> "" Then 
		strGroupName = objExcelInput.Cells(introw, 1).Value
		strDescription = objExcelInput.Cells(intRow, 2).Value
		strGroupOU = objExcelInput.Cells(intRow, 3).Value 
		strMembers = objExcelInput.Cells(intRow, 4).Value
		
		'Remove Spaces from input
		If InStr (strMembers, " ") Then
			strMembers = Replace (strMembers, " ", "")
		End If

		If InStr (strGroupName, " ") Then
			strGroupName = Replace (strGroupName, " ", "_")
		End If		
		
		If InStr (strGroupName,"$iME") = 0 Then
				
			'Start Per User Code	
		    call creategroup()
				
			If strMembers <> "" Then
				WScript.Sleep 500
				Call AddUsers()
			End If
			
		intRow = intRow + 1
		WScript.Echo "intRow = " & introw 
		End If
  	End If
Loop	

	
		
Function creategroup()
WScript.Echo "I'm in createGroup now"
Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &h8

strProvider = "LDAP://mdc/"
strDomain = "dc=domain,dc=com"
strTargetOU = strGroupOu 			' This is the target OU where the group is placed
strobjCategory = "Group"
strobjType = "cn="

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Set objDestOU = GetObject(strProvider & strTargetOU & "," & strDomain)
Set objNewItem = objDestOU.Create(strObjCategory, strobjType & strGroupName)

objNewItem.Put "sAMAccountName", strGroupName
objNewItem.Put "groupType", ADS_GROUP_TYPE_UNIVERSAL_GROUP
objNewItem.Put "Description", strDescription
objNewItem.SetInfo

WScript.Echo "Group Created: " & strGroupName
objConnection.Close
End Function


Function AddUsers()
WScript.Echo "I'm getting ready to add users to: " & strGroupName

strUserArr = Split(strMembers, ";")

' Connect to AD
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

' Get the Group
WScript.Echo "Looking for group " & strGroupName

strCategory = "(objectCategory=Group)"
strTarget = "(cn=" & strGroupName & ")"
strFilter = strCategory & strTarget

objCommand.CommandText = "<LDAP://mdc/dc=domain,dc=com>;(&" & strFilter & ")" & ";DistinguishedName,CN;subtree"
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
  grpLocation = objRecordSet.Fields("DistinguishedName")
  
  WScript.Echo "Group found at " & grpLocation
  
  grpName = objRecordSet.Fields("CN")
  objRecordSet.MoveNext
Wend

Set objGroup = GetObject("LDAP://mdc/" & grpLocation)

' Grab the user and add to group
For i = LBound(strUserArr) to UBound(strUserArr)

strCategory = "(objectCategory=person)"
strClass = "(ObjectClass=user)"
strTarget = "(cn=" & strUserArr(i) & ")"
strFilter = strCategory & strClass & strTarget

objCommand.CommandText = "<GC://dc=domain,dc=com>;(&" & strFilter & ")" & ";DistinguishedName,CN;subtree"
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
  usrLocation = objRecordSet.Fields("DistinguishedName")
  usrName = objRecordset.Fields("CN")
  objRecordSet.MoveNext
Wend

objGroup.add "LDAP://mdc/" & usrLocation
Wscript.Echo "  " & usrName & " has been added to group " & grpName

Next
objConnection.Close
End Function

'objConnection.Close 
objWorkbook.Close 
objExcelInput.Quit

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE