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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something wrong with Loop 2

Status
Not open for further replies.

slint

Technical User
Jul 2, 2006
48
SE
I'm trying to create users and then add the users to a group. It's working greate if i leave the add to group part out.

There is something wrong with the loop in the bottom. On the line from 116 to 119 it defines the group and the path to the group. The first user is ok then it loops and dubble adds the group and path again.

I try to explain it a litle bit more...

The msgBox on line 119 tells me that the group cn=mygroup,ou=mygroupofusers,ou=user,ou=company,dc=mydom,dc=com is going to get users in it. Greate exactly what i want! Then the script loops and comes to the msgBox again and tells me that the path is now cn=mygroup,ou=mygroupofusers,ou=user,ou=company,dc=mydom,dc=com,cn=mygroup,ou=mygroupofusers,ou=user,ou=company,dc=mydom,dc=com and that isnt right and i get an error telling med that the objekt couldent be found.. Is there a flush line i can add to the script for strGroupName or something else maybe..

PS as u can se i have tried to use replace but i only get another error so i have commented it out


Code:
OPTION EXPLICIT


DIM objConn               '
DIM objParent             '
DIM objOu                 '
DIM objGroup              '
DIM objGroupName	  '
DIM objUser               '
DIM rsAnvandare           '
DIM strDbPath             '
DIM strParent             '
DIM strPwd                '
DIM strGroupName          '
DIM strGroupNetBiosName   '
DIM intGroupType          '
DIM strOu                 '
DIM strCN                 '
DIM strSAMName            '
DIM strSAMEnd             '
DIM strUPN                '
DIM strPassword           '
DIM intRaknare            '
DIM strUserName           '
DIM objGroupMember        '


strDbPath = "D:\SHARE\dbAdmin.mdb"
intGroupType = &H80000002   'Global Security
intTest = 0


SET objConn=CreateObject("ADODB.Connection")
objConn.Provider="Microsoft.Jet.OLEDB.4.0"
objConn.Open strDbPath


SET rsTest1 = objConn.Execute("SELECT * FROM tblTest1;")


strGroupName = "cn="&rsTest1("strTest2")
strGroupNetBiosName = rsTest1("strTest2")
strOu = "ou="&rsTest1("strTest2")


strParent = "company"


	IF strParent = "company" THEN
		strPwd = inputbox("password for domain: ?","Account create password","***")

	END IF


	IF NOT strPwd = "" THEN

		IF strParent = "company" THEN
			strParent = "ou=user,ou=company,dc=mydom,dc=com"

		END IF
		

		SET objParent = GetObject ("LDAP://" & strParent)
		SET objTest3 = objParent.Create("organizationalUnit", strTest3)
		

		objTest3.SetInfo
		

		SET objTest3 = NOTHING
		SET objParent =  NOTHING
		

		SET objParent = GetObject ("LDAP://" & strOu&", "& strParent)
		SET objGroup = objParent.Create("group", strGroupName)
		

		objGroup.Put "sAMAccountName", strGroupNetBiosName
		objGroup.Put "groupType", intGroupType
		objGroup.SetInfo
		

		SET objGroup = NOTHING
		SET objParent =  NOTHING



' Beginning of LOOP		

		
		DO UNTIL rsTest1.EOF

				strCN = "cn="&rsTest1("strForNamn")&" "&rsTest1("strEfterNamn")
				strSAMName = rsTest1("strTest2")&rsTest1("strTest1Namn")
				strUPN = strSAMName&"@lbs.se"
				strPassword = rsTest1("strDomainPwd")
				

			SET objParent = GetObject ("LDAP://" & strTest3&", "& strParent)
			SET objUser = objParent.Create("user", strCN)
			

			objUser.Put "sAMAccountName", strSAMName
			objUser.Put "userPrincipalName", strUPN
			
			objUser.SetInfo
			
			objUser.GetInfo
			objUser.Put "displayName", rsTest3("strForNamn") &" "&rsTest1("strEfterNamn")
			objUser.Put "description", rsTest1("strTest2") &" elev"
			objUser.SetPassWord strPassword
			objUser.AccountDisabled = FALSE
			objUser.SetInfo

			

			SET objParent =  NOTHING
			



' The next line gets the path to the group and isnt flushed in the next loop
' So something is wrong here...

			strGroupName = strGroupname &","& strTest3 &","& strParent
			' strGroupName = Replace(strGroupname) ' ","& strTest3 &","& strParent)
			strUserName = strCN &","& strParent
			msgBox strGroupName


			SET objGroupMember = GetObject ("LDAP://" & strGroupName)
			

			objGroupMember.Add(objUser.ADsPath)
			objGroupMember.SetInfo
			

			SET objGroupMember = NOTHING
			SET objUser = NOTHING


			intTest4 = intTest4 + 1
			rsTest1.MoveNext
		LOOP



		MsgBox "SCRIPT DONE "&intTest4&" here here  ",0,"create account done"
		

	ELSE
		MsgBox "No password. Script avslutat",0,"create account done"
	END IF

SET objConn = NOTHING
 
I would suggest you simply break this part off. Create your users first to ensure the objects exist. Then create your groups and then finally join the users to the groups. 3 simple scripts and you probably won't encounter any problems.



I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
[1] The main trouble is that you use the same variable strGroupName at different places and modify it progressively. (In general, reuse the same name is fine, but not like this.)

[1a] At the first record retrieve in the recordset:

> strGroupName = "cn="&rsAnvandare("strKlass")

[1b] Then later use to get the group object using same variable name strGroupName, but now instead of the rdn above it becomes the full dn.

> strGroupName = strGroupname &","& strOu &","& strParent

Furthermore, each time the loop repeat, it just add one prefix more... that's obviously unacceptable.

The resolution is just to use a different variable name for the latter instead of the same.

[2] As a aside, your group info seems to be retrieved from the first record only and being applicable all along. Make sure that is what the data is structured. Also, before getting that info, make sure the recordset is not empty so as to make the script self-contained and more robust.
 

... and there you are again tsuji, you are my saveur.. Realy many thanks for greate replay, it solved the prob. I defined the second strGroupName to strGroupN and voilá.

Thanks again for quick answers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top